我有一个名为header
的表
$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->unique(); <-- Right here
关于 docs它说
Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level. For example, let's define a user_id column on the posts table that references the id column on a users table:
但是在我的 details
表中,我引用了不是主键的 header 中的 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');
最佳答案
也许问题是因为两个表的类型完全相同。我可以看到在第二个表中该字段可以为空,而在第一个表中则不能。也许这就是问题所在
关于php - Laravel 如何从非主键创建外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47446528/