How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

前端 未结 9 1409
执笔经年
执笔经年 2020-11-27 10:21

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 hav

相关标签:
9条回答
  • 2020-11-27 10:44

    If your're looking to set the current datetime for a dateTime column (like i was when I googled), use this way

    $table->dateTime('signed_when')->useCurrent();
    
    0 讨论(0)
  • 2020-11-27 10:47

    As additional possibility for future googlers

    I find it more useful to have null in the updated_at column when the record is been created but has never been modified. It reduces the db size (ok, just a little) and its possible to see it at the first sight that the data has never been modified.

    As of this I use:

    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
    

    (In Laravel 7 with mysql 8).

    0 讨论(0)
  • 2020-11-27 10:55

    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.

    New shortcut

    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();
    

    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'));
    

    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 the useCurrent() 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 timestamps nullable() to allow null values.

    • PostgreSQL & Laravel 4.x

      In Laravel 4.x versions, the PostgreSQL driver was using the default database precision to store timestamp values. When using the CURRENT_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.

      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)'));
      

      Since Laravel 5.0, timestamp() columns has been changed to use a default precision of zero which avoids this.

      Thanks to @andrewhl for pointing out this issue in the comments.

    0 讨论(0)
提交回复
热议问题