gpt4 book ai didi

php - 如何使用具有默认值的 foreignId-constrained?

转载 作者:行者123 更新时间:2023-12-05 09:35:52 26 4
gpt4 key购买 nike

刚刚从视频教程中注意到Laravel有这个foreignId-constrained。所以我尝试将我的代码从 method.1 更改为 method.2,表已迁移,但是当我尝试 db:seed 时,默认值无效(它在下面返回错误)。

(方法一)

$table->unsignedBigInteger('status_id')->default(1);

$table->foreign('status_id')
->references('id')
->on('user_statuses');

(方法二)

$table->foreignId('status_id')->constrained('user_statuses')->default(1);

(使用方法 2 返回错误)

SQLSTATE[HY000]: General error: 1364 Field 'status_id' doesn't have a defaultvalue (SQL: insert into users (username, password) values (admin, $2y$10$SZUIglBQG/HhS/18zn41GOcH8f.hZaNewmyoGJBfDQchfC6OWdx26))

最佳答案

According to the documentation ,您以错误的顺序调用方法:

Any additional column modifiers must be called before the constrained method

$table->foreignId('status_id')->default(1)->constrained('user_statuses');

关于php - 如何使用具有默认值的 foreignId-constrained?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65542026/

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