0
0

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 5 years have passed since last update.

Laravelでポリモーフィック関連させる時にSQLSTATE[42000]: Syntax error or access violation: 1059 エラーが出たときの対処法

0
Posted at

Laravelでポリモーフィック関連を使用する時にmigrationファイルにてmorphsを使用すると自動でインデックスを貼ってくれて非常に便利です。
morphsメソッドの第二引数に何も指定しない場合、インデックスの識別子が自動で作成されますが、テーブル名やカラム名が長いと最大長を超えるため上記エラーが発生します。
morphsの第二引数で任意の識別名を指定してあげれば解決します。

migration.php
 public function up()
    {
        Schema::create('sample_long_table_name_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('text');
            $table->morphs('reportable', 'shorten_identifier');
            $table->timestamps();
        });
    }
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?