2017-01-11 1 views
4

私はLaravelsのデフォルトの移行を使用して通知テーブルを作成しています。Laravel通知テーブルを作成するときに「指定されたキーが長すぎます」

public function up() 
{ 
    Schema::create('notifications', function (Blueprint $table) { 
     $table->uuid('id')->primary(); 
     $table->string('type'); 
     $table->morphs('notifiable'); 
     $table->text('data'); 
     $table->timestamp('read_at')->nullable(); 
     $table->timestamps(); 
    }); 
} 

しかし、それを使用しようとすると、私はエラーを取得する:

[Illuminate\Database\QueryException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `notifications` add index `n 
    otifications_notifiable_id_notifiable_type_index`(`notifiable_id`, `notifiable_type`)) 



    [Doctrine\DBAL\Driver\PDOException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 



    [PDOException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 

更新

私はnotifiable_indexにインデックス列の名前を変更しましたが、長さについて、それはまだ不満インデックスキーの

[Illuminate\Database\QueryException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `notifications` add index `n 
    otifiable_index`(`notifiable_id`, `notifiable_type`)) 



    [Doctrine\DBAL\Driver\PDOException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 



    [PDOException] 
    SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes 
+0

「通知可能な」列の照合とは何ですか? – user3158900

+0

これを解決しましたか? –

+0

@PhilippSanderは短いキーを作成する別の方法を使用しなければなりませんでした。 – naneri

答えて

5

あなたがLaravel 5.4を使用し、5.7.7のリリースよりも古いバージョンのMySQLを実行している場合。これは、AppServiceProviderクラスのbootメソッド内のSchema::defaultStringLengthメソッドを呼び出すことで解決できます。

public function boot() 
{ 
    Schema::defaultStringLength(191); 
} 
+0

https://laravel-news.com/laravel-5-4-key-too-long -error –

+0

は、私はLaravel 5.3 – naneri

+1

@naneriを持っている、あなたはそれを試す? –

0

Laravelは、テーブルと名前が何であるかに基づいて外部キーの表記規則を使用します。しかし、あなたはキーのために独自の名前を指定することができます。

$table->morphs('notifiable', 'mykeyname'); 

そのように、あなたはキーの名前が十分に短いことを確認することができます。

出典:https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Schema/Blueprint.php#L963

+0

私は列名を更新しましたが、それでもキーの長さについては苦情を言います。(。最初の投稿で更新を見ることができます。 – naneri

0

最後に、通知可能なIDの手動インデックスを作成しました。

Schema::create('notifications', function (Blueprint $table) { 
     $table->uuid('id')->primary(); 
     $table->string('type'); 
     $table->text('data'); 
     $table->timestamp('read_at')->nullable(); 
     $table->timestamps(); 

     $table->unsignedInteger("notifiable_id"); 
     $table->string("notifiable_type"); 
     $table->index('notifiable_id'); 
    }); 

代わりのコマンドを有する:ここで、移行され

$table->morphs('notifiable'); 

をI 3線を有する:

$table->unsignedInteger("notifiable_id"); 
    $table->string("notifiable_type"); 
    $table->index('notifiable_id'); 

欠点は、インデックスはタイプに基づいて合成されないことです+ id、idにのみ基づいています。しかし、私は通知できるユーザーモデルしかないので、それは私にとっては大丈夫です。

関連する問題