Laravel Database Migrations中的 timestamps 與 nullableTimestamps

Laravel Database Migrations中的 timestamps 與 nullableTimestamps

才知道Laravel Database Migrations 中的 timestampsnullableTimestamps 在版本5.5之後數據庫中建立的是相同類型和屬性的column!!!好坑啊!!!

原文件(vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php)中指出:

    /**
     * Add nullable creation and update timestamps to the table.
     *
     * @param  int  $precision
     * @return void
     */
    public function timestamps($precision = 0)
    {
        $this->timestamp('created_at', $precision)->nullable();

        $this->timestamp('updated_at', $precision)->nullable();
    }

    /**
     * Add nullable creation and update timestamps to the table.
     *
     * Alias for self::timestamps().
     *
     * @param  int  $precision
     * @return void
     */
    public function nullableTimestamps($precision = 0)
    {
        $this->timestamps($precision);
    }

文檔也變成了:

Command Description
$table->nullableTimestamps(); Alias of timestamps()method.
$table->timestamps(); Adds nullable created_at and updated_at TIMESTAMP equivalent columns.

處理這倆個field的默認值是在Eloquent中,所以如果你有table不是對應Eloquent ORM的話,要記得自己手動插入值,或者從數據庫層給這倆個字段增加一個默認值。

PS. MYSQL 5.7版本timestamp類型的字段設置默認值的時候會遇到報錯,在這個issue中有提及( https://github.com/cakephp/phinx/issues/1531 ),目前還沒找到這個問題的解決方案,歡迎大家提供。

Y Cheung

Y Cheung

Blogger, Programer & Traveler.
Shanghai