20
15

More than 5 years have passed since last update.

Laravelのマイグレーションではtimestamp型のchangeが出来ない

Posted at

エラー内容

Laravelのマイグレーションでtimestamp型のカラムに変更を加えようとすると以下のようなエラーが出る。

Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \  
  Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Ty  
  pes\Type::getTypesMap(). If this error occurs during database introspection then you might have forgo  
  tten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMap  
  ping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty y  
  ou might have a problem with the cache or forgot some mapping information.

理由

Laravel内で利用しているDoctrineというライブラリが対応していないから。ちなみに対応の予定もなさそう。
https://github.com/doctrine/dbal/issues/2558

理由としてはtimestamp型はMySQL固有の型だから特定のDBサービス用の型はサポートしないよということ。

ちなみにLaravelのchangeで変更できるカラムの種類は以下に限られている

bigInteger
binary
boolean
date
dateTime
dateTimeTz
decimal
integer
json
longText
mediumText
smallInteger
string
text
time
unsignedBigInteger
unsignedInteger
unsignedSmallInteger

解決策

DBファサードを使って生のSQLを発行する
MySQLのバージョンは5.7.25です。


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        DB::statement('ALTER TABLE `テーブル名` MODIFY COLUMN `カラム名` TIMESTAMP NULL;');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('ALTER TABLE `テーブル名` MODIFY COLUMN `カラム名` TIMESTAMP NOT NULL;');
    }
20
15
1

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
20
15