1

I'm trying to create a pivot table to hold some relationship data for some basic ACL functionality.

The migration class:

Schema::create('group_user', function($table)
{
    $table->increments('id');
    $table->unsignedInteger('group_id');
    $table->unsignedInteger('user_id');
    $table->timestamps();
    $table->softDeletes();
});

Schema::table('group_user', function($table)
{
    $table->foreign('group_id')
        ->reference('id')->on('groups');
    $table->foreign('user_id')
        ->reference('id')->on('users');
});

After running the migration command, I get the following error:

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at li
  ne 1 (SQL: alter table `group_user` add constraint group_user_group_id_foreign foreign key (`group_id`) references `groups` ())

As you can see, the SQL syntax to add the foreign key constraint is missing the 'id' column name for the referenced table. Is this a bug in Laravel or is there something wrong with my schema code?

2 Answers 2

2

So I finally figured it out and now I feel really stupid. It was a typo error on my part.

It should have been references() instead of reference().

Sign up to request clarification or add additional context in comments.

Comments

0

It is a syntax error with an alter table on the "group_user" table which is not referenced in your code.

1 Comment

Sorry looks like I pasted the wrong migration code. I've edited my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.