gpt4 book ai didi

php - Laravel:使用主索引作为外键?

转载 作者:行者123 更新时间:2023-12-01 00:36:30 24 4
gpt4 key购买 nike

我在模型 user 和模型 profilePresi 之间有一个 hasOne 关系。这意味着每个用户都有 0 或 1 个总统设置。因此,profilePresi 表的主索引与外键相同。

这就是我想添加外键的方式:

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');
});

但是我得到以下错误:

enter image description here

我该如何解决?我正在使用 InnoDB 引擎,所以我知道我可以将索引键设置为外键。

最佳答案

而不是使用

$table->integer('idMember');

使用

table->unsignedInteger('idMember');

关于php - Laravel:使用主索引作为外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420676/

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