常规错误:1364字段'标识符'没有默认值

时间:2017-03-26 20:42:49

标签: php mysql sql laravel

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('identifier')->unique();
    $table->string('username')->unique();
    $table->string('name');
    $table->string('avatar');
    $table->string('trade')->nullable();
    $table->decimal('funds')->default(0);
    $table->enum('visibility', [1, 2, 3]);
    $table->uuid('api_token');
    $table->timestamps();
});

User::updateOrCreate([
    'identifier' => 'dasdasd',
    'username' => $user->nickname,
    'name' => $user->name,
    'avatar' => $user->avatar,
    'visibility' => $user->visibility,
    'api_token' => Uuid::generate()
]);

结果:SQLSTATE [HY000]:常规错误:1364字段'标识符'没有默认值(SQL:插入usersnameupdated_at,{{ 1}})价值观(GuilhermeAraújo,2017-03-26 20:39:04,2017-03-26 20:39:04))

有什么问题?

2 个答案:

答案 0 :(得分:3)

您应该为identifier字段生成唯一的整数:

'identifier' => 12345,

由于您使用的是updateOrCreate(),因此您应该将identifier添加到$fillable数组中:

protected $fillable = ['identifier', ....];

答案 1 :(得分:0)

如果您的变量标识符是用户可填充的,则必须用整数值填充。