3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP のせいでtimestampのカラムが勝手に更新されてしまう

Posted at

timestampはデフォルトで, CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP属性がつく

マイグレーションファイルでtimestampのカラムを設定すると, レコードを更新すると日付が自動更新されるようになっています. DBを確認すると, カラムの属性にCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPがついています. これを消せばカラムが自動更新することがなくなります.

migrate.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestTable extends Migration
{
    public function up() {
        Schema::create('test_tables', function (Blueprint $table) {
            $table->timestamp('output_at')->comment('出力日時');
        });
    }

// 略
}

nullableにするだけ

マイグレーションファイルのカラムを設定するときにnullableと追加すると, CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPが消えるようになります.

migrate_new.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class TestTable extends Migration
{
    public function up() {
        Schema::create('test_tables', function (Blueprint $table) {
            $table->timestamp('output_at')->nullable()->comment('出力日時');
        });
    }

// 略
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?