简体   繁体   中英

Laravel 4 Migration has syntax error when adding Foreign Key

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?

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() .

它是语法错误,其中“ group_user”表上的alter表未在您的代码中引用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM