gpt4 book ai didi

php - 流明自动增量关系创建保持在 0

转载 作者:行者123 更新时间:2023-12-03 19:31:03 24 4
gpt4 key购买 nike

我有课Chat :

class Chat extends Model
{
protected $primaryKey = 'chat_id';
protected $guarded = [];

public function comment()
{
return $this->hasOne(Comments::class, 'comment_id', 'comment_id');
}
}

还有一个类 Comments :
class Comments extends Model
{
protected $primaryKey = 'comment_id';
protected $guarded = [];
}

这是 Comments 的迁移 table :
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('comment_id');
$table->integer('user_id')->unsigned()->index('comments_user_id');
$table->boolean('mark');
$table->string('nick_name', 15)->nullable();
$table->text('comment', 65535);
});
}

Chat有一个名为 comment_id 的列.

当我创建 Comments使用 Eloquent基本方法,一切都很好, Comments.comment_id从 1 开始,下一个 Comments.comment_id列将增加:
$comment = Comments::create([
'user_id' => 1,
'mark' => 50,
'comment' => 'Lorem ipsum dolor es at'
]); // $comment->comment_id = 1

但是每当我通过 Chat 创建它时关系, Comments.comment_id总是 等于 0 :
$comment = $chat->comment()->create([
'user_id' => 1,
'mark' => 50,
'comment' => 'Lorem ipsum dolor es at',
]); // $comment->comment_id = 0

我说 总是 , 因为创建了两个 Comments在具有该关系的一行中,将在 Comments.comment_id 上引发 UNIQUE 约束异常。 field 。

我的配置有错误吗?
顺便说一句,我正在使用 SQLite(这是在测试时发生的),如果有任何兴趣的话。

最佳答案

Chat 关系应该是belongsTo 关系,而不是hasOne。

关于php - 流明自动增量关系创建保持在 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41082320/

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