gpt4 book ai didi

php - Laravel 如何从非主键创建外键约束

转载 作者:可可西里 更新时间:2023-11-01 08:39:01 25 4
gpt4 key购买 nike

我有一个名为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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com