[英]Laravel how to create foreign key constrain from non-primary key
我有一个名为header
的表
$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->unique(); <-- Right here
在文档上说
Laravel还支持创建外键约束,这些约束用于强制数据库级别的引用完整性。 例如,让我们在posts表上定义一个user_id列,该列引用users表上的id列:
但是在我的details
表上,我引用了不是主键的header_id中的transaction_id,并且我收到错误此错误(errno: 150 "Foreign key constraint is incorrectly formed")
。 如何从非主要引用它
$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->nullable();
$table->foreign('transaction_id')->references('transaction_id')->on('header');
也许问题是因为两个表都是完全相同的类型。 我可以看到,在第二个表中,该字段可以为空,而在第一个表中则不是。 也许这就是问题所在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.