You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Eloquent主键未生效:AcctId未设为accounts表主键排查

问题解决:AcctId未被设置为主键的原因及修复步骤

你的模型配置是正确的,但数据库层面的主键需要在迁移文件中明确声明——当前迁移代码仅创建了AcctId字段,并未将它标记为主键,这就是问题所在。

修复步骤

  1. 修改迁移文件,在创建AcctId字段时调用primary()方法,将其设为主键
  2. 重新执行迁移命令:php artisan migrate:fresh

修改后的迁移代码示例

public function up(): void
{
    Schema::create('accounts', function (Blueprint $table) {
        $table->string('AcctId')->primary(); // 新增primary()标记主键
        $table->string('AccountName');
        $table->string('Address');
        $table->string('City');
        $table->string('State');
        $table->string('Zip');
        $table->timestamps();
    });
}

补充说明

模型中的$primaryKey、$incrementing、$keyType是告诉Laravel ORM如何识别和处理主键,但数据库表结构的主键必须在迁移中定义,两者配合才能让主键正常工作。

内容的提问来源于stack exchange,提问作者bart2puck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 16:47:01