Laravel error in creating a table with two timestamp columns(Laravel 在创建具有两个时间戳列的表时出错)
问题描述
我在 Laravel 6.6 中创建了一个表,其定义如下.
I created a table in Laravel 6.6 with the following definition.
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');
});
}
当我运行迁移命令时,出现以下错误.
When I run the migration command I get the following error.
IlluminateDatabaseQueryException : SQLSTATE[42000]: 语法错误或访问冲突:1067 'valid_to' 的默认值无效(SQL:创建表 quarters
(quarter_id
int unsigned not null, year
整数无符号not null, quarter_num
int unsigned not null, valid_from
timestamp not null, valid_to
timestamp not null, description
varchar(255) null, created_at
时间戳 null, updated_at
时间戳null) 默认字符集 utf8mb4 collate 'utf8mb4_unicode_ci')
IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'valid_to' (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')
这里是 Eloquent 生成的 SQL:
here is the SQL generated by Eloquent:
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
列!
The strange thing is that if I comment out the valid_to
line then it creates the table with no error. But the definition of valid_to
is 100% similar to valid_from
, and it does not throw that error for the valid_from
column. Actually it seems the DB does not allow for twotimestamp
columns!
按照评论中的要求,我运行了 php artisan migrate --pretend
,结果如下:
As requested in the comments I ran the php artisan migrate --pretend
and here is the result:
C:xampphtdocsvoiceit> 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`)
推荐答案
我通过将列的类型从 timestamp
更改为 dateTime
解决了我的问题.因此,如下更改表定义解决了我的问题,因为我需要一个日期和时间:
I solved my issue by changing the type of the column from timestamp
to dateTime
. So changing the table definition as follow solved my issue as I need a date and a time:
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');
});
但是,我仍然想知道如何在一个表中拥有多个 not null
时间戳列.
However, I am still interested to know how can we have multiple not null
timestamp columns in a table.
这篇关于Laravel 在创建具有两个时间戳列的表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 在创建具有两个时间戳列的表时出错
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01