[英]Laravel 4 Migration has syntax error when adding Foreign Key
我试图创建一个数据透视表来保存一些基本ACL功能的关系数据。
迁移类:
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');
});
运行迁移命令后,出现以下错误:
[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` ())
如您所见,添加外键约束的SQL语法缺少所引用表的'id'列名称。 这是Laravel中的错误,还是我的架构代码有问题?
所以我终于想通了,现在我真的很傻。 这是我的错字。
应该是references()
而不是reference()
。
它是语法错误,其中“ group_user”表上的alter表未在您的代码中引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.