【发布时间】:2021-04-15 06:41:52
【问题描述】:
我正在创建一个带有 2 个时间戳的迁移,但由于某种原因,我不能在没有 default/nullable 值的情况下放置 2 个 timestamps。
我的代码:
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, `checkout` timestamp not null, `checkin` 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')
我认为这可能是 Laravel 中的一个错误。
- 注意 1:如果我删除不可为空的“第二个”时间戳,则可以正常工作。
- 注意 2:我使用
php artisan migration:fresh来获得这些结果。
【问题讨论】:
标签: eloquent laravel-8 laravel-migrations php-7.4