Laravel 在创建具有两个时间戳列的表时出错
问题描述
我在 Laravel 6.6 中创建了一个表,其定义如下.
I created a table in Laravel 6.6 with the following definition.
当我运行迁移命令时,出现以下错误.
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:
奇怪的是,如果我注释掉 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:
推荐答案
我通过将列的类型从 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:
但是,我仍然想知道如何在一个表中拥有多个 not null
时间戳列.
However, I am still interested to know how can we have multiple not null
timestamp columns in a table.
这篇关于Laravel 在创建具有两个时间戳列的表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!