【发布时间】:2019-12-13 15:33:20
【问题描述】:
我在 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_idint unsigned not null,year整数无符号 非空,quarter_numint 无符号非空,valid_from时间戳非空,valid_to时间戳非空,descriptionvarchar(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 的定义与valid_from 100% 相似,并且它不会为valid_from 列抛出该错误。实际上,数据库似乎不允许两个timestamp 列!
按照 cmets 的要求,我运行了 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`)
【问题讨论】:
-
需要让它们可以为空:` $table->timestamp('valide_to')->nullable();`
-
但我不希望它们可以为空。我需要来自用户的价值。是数据库的一些限制吗?另外,如果是这种情况,为什么它接受
valid_from不为空? -
这实际上是一个非常奇怪的场景。一个 nullableTimestamp 似乎以某种方式解决了这个问题,但它是在对抗症状,而不是找到真正问题的解决方案。你能运行
php artisan migrate --pretend并将输出添加到问题中吗?看起来 Laravel 可能会在这里创建错误的 SQL,因为理论上它应该添加一个 CURRENT_TIMESTAMP 作为 SQL 的默认值(参见 github.com/laravel/framework/blob/…) -
@Stratadox 我添加了。此外,我在 PhpMyAdmin 中运行 SQL 语句,然后收到相同的错误:
MySQL said: #1067 - Invalid default value for 'valid_to'。这似乎是一个错误的 SQL 或数据库的限制。 -
你用的是哪个Mysql版本?看起来explicit_defaults_for_timestamp 设置可能会导致这个问题:dev.mysql.com/doc/refman/5.6/en/…
标签: php eloquent mariadb laravel-6