简体   繁体   中英

General error: 1364 Field 'identifier' doesn't have a default value

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();
});

and

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

Results: SQLSTATE[HY000]: General error: 1364 Field 'identifier' doesn't have a default value (SQL: insert into users ( name , updated_at , created_at ) values (Guilherme Araújo, 2017-03-26 20:39:04, 2017-03-26 20:39:04))

What is wrong?

You should generate an unique integer for the identifier field:

'identifier' => 12345,

And since you're using updateOrCreate() you should add identifier to the $fillable array:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM