Mah*_*ari 4 php mariadb eloquent laravel-6
我在 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');
});
}
Run Code Online (Sandbox Code Playgroud)
当我运行迁移命令时,出现以下错误。
Illuminate\Database\QueryException : SQLSTATE[42000]:语法错误或访问冲突:1067 'valid_to' 的默认值无效(SQL:创建表
quarters(quarter_idint unsigned not null、yearint unsigned not null、quarter_numint unsigned not null、valid_fromtimestamp not null、valid_to时间戳不为空、descriptionvarchar(255) 为空、created_at时间戳为空、updated_at时间戳为空) 默认字符集 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'
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我注释掉该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`)
Run Code Online (Sandbox Code Playgroud)
我通过将列的类型从 更改为 解决了我的timestamp问题dateTime。因此,如下更改表定义解决了我的问题,因为我需要日期和时间:
Schema::create('quarters', function (Blueprint $table) {
$table->integer('quarter_id')->unsigned();
$table->integer('year')->unsigned();
$table->integer('quarter_num')->unsigned();
$table->dateTime('valid_from');
$table->dateTime('valid_to');
$table->string('description')->nullable();
$table->timestamps();
$table->primary('quarter_id');
});
Run Code Online (Sandbox Code Playgroud)
但是,我仍然有兴趣知道如何not null在表中拥有多个时间戳列。
| 归档时间: |
|
| 查看次数: |
7297 次 |
| 最近记录: |