LoginSignup
11
4

More than 3 years have passed since last update.

【Laravel】migrationでカラムの変更に対応してないデータ型があった

Last updated at Posted at 2020-04-13

はじめに

Laravelのmigration機能についてです。
migration機能では、DBのテーブル作成と削除やカラムの追加と削除などの操作を行うことができます。

カラムの変更では、カラムをnullableに変更したり、データ型を変更したりできます。

ある時、一部のデータ型でカラムの変更を行おうとした時にエラーが発生したので、
カラムの変更に対応してないデータ型があることを知りました。
あまり知られていないようなので、まとめます。

カラムの変更方法について

カラムを変更するには,作成と同じようにカラム名を指定した後ろでchange()を呼びます。
例にあるようにInteger型などは普通に変更できます。

    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->unsignedInteger('column_a')->nullable()->change();
        });
    }

対応してない方を変更しようとした時に発生するエラー

対応してないデータ型でchangedを呼ぶと以下のようなmigration実行時にエラーが発生しました。

In DBALException.php line 264:

  Unknown column type "tinyinteger" 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 \Doct  
  rine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#r  
  egisterDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping informa  
  tion.           

変更できるデータ型

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

参考

Only the following column types can be "changed": bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger and unsignedSmallInteger.

変更できないデータ型

tinyInteger,unsignedTinyIntegerなど。変更できるカラム以外

こういうカラムは、一回dropしてからまた作り直す必要がありそうです。
(他に良いやり方があったら教えてください)

以上、参考になりましたら幸いです。

11
4
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
11
4