2015-05-11 129 views
11

我試圖運行以下遷移:Laravel 5遷移標識符名稱太長

public function up() 
{ 
    Schema::create('lifestyle_questions', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->string('question'); 
     $table->timestamps(); 
    }); 

    Schema::create('lifestyle_question_answers', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->integer('lifestyle_question_id')->unsigned(); 
     $table->foreign('lifestyle_question_id')->references('id')->on('lifestyle_questions'); 
     $table->string('answer'); 
     $table->timestamps(); 
    }); 

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table) 
    { 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->integer('lifestyle_question_answer_id')->unsigned(); 
     $table->foreign('lifestyle_question_answer_id')->references('id')->on('lifestyle_question_answers'); 
    }); 
} 

,但我得到了以下錯誤:

[Illuminate\Database\QueryException] 
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long (SQL: alter table `user_lifestyle_question_answers` add constraint user_lifestyle_question_answers_lifestyle_question_answer_id_foreign foreign key (`lifestyle_question_answer_id`) references `lifestyle_question_answers` (`id`)) 

[PDOException] 
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long 

回答

37

您可以通過自定義索引名作爲第二參數放入foreign()方法中。或者只是使用較短的表/列名稱。

所以,你想要做的事,如:

public function up() 
{ 
    Schema::create('lifestyle_questions', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->string('question'); 
     $table->timestamps(); 
    }); 

    Schema::create('lifestyle_question_answers', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->integer('lifestyle_question_id')->unsigned(); 
     $table->foreign('lifestyle_question_id', 'lq_id_foreign')->references('id')->on('lifestyle_questions'); 
     $table->string('answer'); 
     $table->timestamps(); 
    }); 

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table) 
    { 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->integer('lifestyle_question_answer_id')->unsigned(); 
     $table->foreign('lifestyle_question_answer_id', 'lqa_id_foreign')->references('id')->on('lifestyle_question_answers'); 
    }); 
} 
+0

的伎倆感謝那確實! – geoffs3310

+1

僅供參考:根據http://dev.mysql.com/doc/refman/5.5/en/identifiers.html,MySQL標識符應始終小於64個字符 –