问题描述
我正在创建一个带有 2 个时间戳的迁移,但由于某种原因,我不能在没有 timestamps
/default
值的情况下放置 2 个 nullable
。
我的代码:
Schema::create('lessons',function (Blueprint $table) {
$table->id();
$table->timestamp('checkin');
$table->timestamp('checkout');
$table->string('url')->nullable();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->softDeletes();
});
如果我在 checkout
: $table->timestamp('checkout')->nullable;
中放置了一个可为空的值,我的代码工作正常。但是我的两个值不能为空。
错误:
Illuminate\Database\QueryException
sqlSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkout' (sql: create table `lessons` (`id` bigint unsigned not null auto_increment primary key,`checkin` timestamp not null,`checkout` timestamp not null,`url` varchar(191) null,`created_at` timestamp not null default CURRENT_TIMESTAMP,`updated_at` timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,`deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
如果更改顺序,错误也会发生在签入时
sqlSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkin' (sql: create table `lessons` (`id` bigint unsigned not null auto_increment primary key,`deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
解决方法
我用这篇文章解决了我的问题: https://stackoverflow.com/a/59326426/11639058
我已将 timestamp
更改为 dateTime
。