如何通过Laravel迁移将时间戳列的默认值设置为当前时间戳? [英] How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?
问题描述
我想使用Laravel Schema Builder/Migrations创建一个默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
的时间戳列.我已经多次浏览过Laravel文档,但没有看到如何将它作为timestamp列的默认值.
I would like to make a timestamp column with a default value of CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
using the Laravel Schema Builder/Migrations. I have gone through the Laravel documentation several times, and I don't see how I can make that the default for a timestamp column.
timestamps()
函数将其创建的两列设置为默认值0000-00-00 00:00
.
The timestamps()
function makes the defaults 0000-00-00 00:00
for both columns that it makes.
推荐答案
鉴于这是一个原始表达式,您应使用DB::raw()
将CURRENT_TIMESTAMP
设置为列的默认值:
Given it's a raw expression, you should use DB::raw()
to set CURRENT_TIMESTAMP
as a default value for a column:
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
这在每个数据库驱动程序上都可以正常工作.
This works flawlessly on every database driver.
新快捷方式
从Laravel 5.1.25开始(请参见 PR 10962 和
As of Laravel 5.1.25 (see PR 10962 and commit 15c487fe) you can use the new useCurrent()
column modifier method to set the CURRENT_TIMESTAMP
as a default value for a column:
$table->timestamp('created_at')->useCurrent();
回到问题所在,在MySQL上,您还可以通过DB::raw()
使用ON UPDATE
子句:
Back to the question, on MySQL you could also use the ON UPDATE
clause through DB::raw()
:
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
陷阱
-
MySQL
从MySQL 5.7开始,0000-00-00 00:00:00
不再被视为有效日期.如 Laravel 5.2升级指南所述,所有时间戳列均应包含一个将记录插入数据库时的有效默认值.您可以在迁移中使用useCurrent()
列修饰符(从Laravel 5.1.25及更高版本开始)将timestamp列默认为当前时间戳,或者可以将timestampsnullable()
设置为允许空值.Gotchas
MySQL
Starting with MySQL 5.7,0000-00-00 00:00:00
is no longer considered a valid date. As documented at the Laravel 5.2 upgrade guide, all timestamp columns should receive a valid default value when you insert records into your database. You may use theuseCurrent()
column modifier (from Laravel 5.1.25 and above) in your migrations to default the timestamp columns to the current timestamps, or you may make the timestampsnullable()
to allow null values.PostgreSQL& Laravel 4.x
在Laravel 4.x版本中,PostgreSQL驱动程序使用默认的数据库精度来存储时间戳值.在具有默认精度的列上使用CURRENT_TIMESTAMP
函数时,PostgreSQL会生成具有更高可用精度的时间戳,从而生成带有小数第二部分的时间戳-PostgreSQL & Laravel 4.x
In Laravel 4.x versions, the PostgreSQL driver was using the default database precision to store timestamp values. When using theCURRENT_TIMESTAMP
function on a column with a default precision, PostgreSQL generates a timestamp with the higher precision available, thus generating a timestamp with a fractional second part - see this SQL fiddle.这将导致Carbon无法解析时间戳,因为它预计不会存储微秒.为避免这种意外行为破坏您的应用程序,您必须对
CURRENT_TIMESTAMP
函数明确给出零精度,如下所示:This will led Carbon to fail parsing a timestamp since it won't be expecting microseconds being stored. To avoid this unexpected behavior breaking your application you have to explicitly give a zero precision to the
CURRENT_TIMESTAMP
function as below:$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP(0)'));
从Laravel 5.0版本开始,
timestamp()
列已更改为使用默认精度零,从而避免了这种情况.Since Laravel 5.0,
timestamp()
columns has been changed to use a default precision of zero which avoids this.感谢 @andrewhl 在评论中指出此问题.
Thanks to @andrewhl for pointing out this issue in the comments.
这篇关于如何通过Laravel迁移将时间戳列的默认值设置为当前时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!