问题描述
我在模型user
和模型profilePresi
之间具有hasOne
关系.这意味着每个用户都有0个或1个总裁设置.因此,profilePresi
表的主索引与外键相同.
I have a hasOne
relation between the model user
and the model profilePresi
. This means each user has either 0 or 1 president settings. Thus, the primary index of the profilePresi
table is identical to the foreign key.
这就是我要添加外键的方式:
This is how I wanted to add the foreign key:
Schema::create('profilePresi', function (Blueprint $table) {
$table->integer('idMember');
$table->primary('idMember');
$table->string('idCountry',2);
$table->timestamps();
});
Schema::table('profilePresi', function($table) {
$table->foreign('idMember')
->references('id')->on('users')
->onDelete('cascade');
});
但是我遇到以下错误:
我该如何解决?我正在使用InnoDB引擎,所以我知道可以将索引键设置为外键.
How can I fix it? I am using InnoDB engine so I know that I am allowed to set index key as foreign key.
推荐答案
您的users.id列可能是无符号整数,但是您的profilePresi.idMember只是一个整数.要用作外键,它们都必须是未签名的或未签名的.检查是否是这种情况,如果是,只需使profilePresi.idMember也未签名-应该这样做. :)
Your users.id column is likely an unsigned integer, but your profilePresi.idMember is just an integer. To use as a foreign key, they both need to be either unsigned or not. Check if that's the case, and if so, just make the profilePresi.idMember unsigned as well - that should do it. :)
这篇关于Laravel:使用主索引作为外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!