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

调用storeRegistrationInfoF方法插入数据时为何报‘字段无默认值’错误?

问题原因与解决方案

嘿,这个报错其实指向的问题很清晰,咱们来一步步捋清楚:

这个错误的核心原因是:你要插入数据的registrations表中,participant_that_did_registration字段被设置了非空(NOT NULL)约束,但它既没有默认值,你这次的INSERT语句里也没给这个字段传值,数据库直接拒绝了这次插入操作。

下面是几个常见的场景和对应的解决办法:

  • 场景1:你漏传了这个字段的值
    先检查你的Ajax POST请求,确认前端有没有把participant_that_did_registration对应的数据传过来(比如如果是登录用户ID,前端可能没携带这个参数)。另外还要检查你的Laravel Registration模型的$fillable属性——如果这个字段没在$fillable列表里,Laravel的批量赋值机制会自动过滤掉它,导致插入时没传值。

    模型里的正确写法应该是这样:

    class Registration extends Model
    {
        protected $fillable = [
            'congress_id',
            'status',
            'participant_that_did_registration', // 必须把这个字段加进来
        ];
    }
    
  • 场景2:业务上允许这个字段为空
    如果你的逻辑允许participant_that_did_registration暂时为空,可以修改数据库迁移文件,把这个字段设置为可空,然后重新执行迁移(记得先备份数据!):

    Schema::table('registrations', function (Blueprint $table) {
        $table->integer('participant_that_did_registration')->nullable()->change();
    });
    

    然后运行迁移命令:php artisan migrate

  • 场景3:这个字段应该有默认值
    如果这个字段有固定的默认值(比如当前登录用户的ID),你可以在插入时手动赋值:

    $registration = new Registration();
    $registration->congress_id = $request->congress_id;
    $registration->status = 'C';
    $registration->participant_that_did_registration = auth()->id(); // 手动设置当前用户ID
    $registration->save();
    

    或者在模型里直接设置默认属性:

    class Registration extends Model
    {
        protected $attributes = [
            'participant_that_did_registration' => 1, // 这里填写你需要的默认值
        ];
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:09:33