我在 Laravel 6.6 中创建了一个具有以下定义的表。
public function up()
{
Schema::create('quarters', function (Blueprint $table) {
$table->integer('quarter_id')->unsigned();
$table->integer('year')->unsigned();
$table->integer('quarter_num')->unsigned();
$table->timestamp('valid_from');
$table->timestamp('valid_to'); // <------ error on this line
$table->string('description')->nullable();
$table->timestamps();
$table->primary('quarter_id');
});
}
当我运行迁移命令时,出现以下错误。
Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或
访问冲突:1067 “valid_to”的默认值无效(SQL:
创建表quarters
(quarter_id
int 无符号不为空,year
无符号整型
不为空,quarter_num
int 无符号不为空,valid_from
时间戳不为空,valid_to
时间戳不为空,description
varchar(255) 空,created_at
时间戳为空,updated_at
时间戳
null) 默认字符集 utf8mb4 collate 'utf8mb4_unicode_ci')
这是 Eloquent 生成的 SQL:
CREATE TABLE `quarters`(
`quarter_id` INT UNSIGNED NOT NULL,
`year` INT UNSIGNED NOT NULL,
`quarter_num` INT UNSIGNED NOT NULL,
`valid_from` TIMESTAMP NOT NULL,
`valid_to` TIMESTAMP NOT NULL,
`description` VARCHAR(255) NULL,
`created_at` TIMESTAMP NULL,
`updated_at` TIMESTAMP NULL
) DEFAULT CHARACTER SET utf8mb4 COLLATE 'utf8mb4_unicode_ci'
奇怪的是,如果我注释掉valid_to
行,然后它会毫无错误地创建表。但定义valid_to
100%相似于valid_from
,并且它不会抛出该错误valid_from
柱子。实际上,数据库似乎不允许两个timestamp
列!
根据评论中的要求,我运行了php artisan migrate --pretend
这是结果:
C:\xampp\htdocs\voiceit> php artisan migrate --pretend
CreateQuartersTable: create table `quarters` (`quarter_id` int unsigned not null, `year` int unsigned not null, `quarter_num` int unsigned not null, `valid_from` timestamp not null, `valid_to` timestamp not null, `description` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
CreateQuartersTable: alter table `quarters` add primary key `quarters_quarter_id_primary`(`quarter_id`)
CreatePeopleDatasTable: create table `people_datas` (`mt_id` bigint unsigned not null, `valid_for` int unsigned not null, `local_personal_id` bigint unsigned not null, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `date_of_birth` date null, `date_of_join` date null, `gender` varchar(1) not null, `location_type` varchar(1) not null, `created_at` timestamp null, `updated_at` timestamp null, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
CreatePeopleDatasTable: alter table `people_datas` add primary key `people_datas_mt_id_valid_for_primary`(`mt_id`, `valid_for`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_valid_for_foreign` foreign key (`valid_for`) references `quarters` (`quarter_id`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_gender_foreign` foreign key (`gender`) references `genders` (`id`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_location_type_foreign` foreign key (`location_type`) references `location_types` (`id`)
CreatePeopleDatasTable: alter table `people_datas` add index `people_datas_last_name_index`(`last_name`)