2016-08-30 72 views
1

我已將遷移文件添加到數據庫中現有列的索引。這是我的移民文件:Laravel - 遷移(向表中添加索引會引發錯誤)

public function up() 
    { 
     Schema::table('alternatives', function (Blueprint $table){ 
      $table->index('question_id'); 
      $table->index('correct'); 
     }); 

     Schema::table('answers', function (Blueprint $table){ 
      $table->index(['question_id', 'player_id']); 
      $table->index('quiz_id'); 
     }); 

     Schema::table('players', function (Blueprint $table){ 
      $table->index('nickname'); 
     }); 

     Schema::table('player_quiz', function (Blueprint $table){ 
      $table->index('player_id'); 
      $table->index('quiz_id'); 
     }); 

     Schema::table('question_quiz', function (Blueprint $table){ 
      $table->index('question_id'); 
      $table->index('quiz_id'); 
      $table->index('start_time'); 
      $table->index('active_time'); 
      $table->index('finish_time'); 
     }); 

     Schema::table('question_subject', function (Blueprint $table){ 
      $table->index('question_id'); 
      $table->index('subject_id'); 
     }); 

     Schema::table('question_topic', function (Blueprint $table){ 
      $table->index('question_id'); 
      $table->index('topic_id'); 
     }); 

     Schema::table('question_year', function (Blueprint $table){ 
      $table->index('question_id'); 
      $table->index('year_id'); 
     }); 

     Schema::table('quiz_subject', function (Blueprint $table){ 
      $table->index('quiz_id'); 
      $table->index('subject_id'); 
     }); 

     Schema::table('quiz_topic', function (Blueprint $table){ 
      $table->index('quiz_id'); 
      $table->index('topic_id'); 
     }); 

     Schema::table('quiz_year', function (Blueprint $table){ 
      $table->index('quiz_id'); 
      $table->index('year_id'); 
     }); 

     Schema::table('quizzes', function (Blueprint $table){ 
      $table->index('code'); 
      $table->index('token'); 
      $table->index('status'); 
     }); 

     Schema::table('subjects', function (Blueprint $table){ 
      $table->index('name'); 
     }); 

     Schema::table('topics', function (Blueprint $table){ 
      $table->index('name'); 
     }); 

     Schema::table('years', function (Blueprint $table){ 
      $table->index('name'); 
     }); 
    } 

但是當我運行php artisan migrate我得到一個錯誤:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'alternatives_question_id_index' (SQL: alter table alternatives add index alternatives _question_id_index (question_id))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'alternatives_question_id_index'

這是奇怪的,因爲我沒有當我有替代品表中已經存在的指數,甚至刪除該行,我也收到'correct'列的錯誤,該列也沒有以前的任何索引。我也得到了'answers'表中所有列的相同錯誤。

我使用Laravel 5.2和mysql版本14.14。

這是一個用於創建'alternatives'表我的移民文件看起來像:

public function up() 
    { 
     Schema::create('alternatives', function (Blueprint $table) { 
      $table->timestamps(); 
      $table->increments('id'); 
      $table->string('text'); 
      $table->boolean('correct'); 
      $table->integer('question_id'); 
     }); 


     Schema::table('alternatives', function ($table) { 
      $table->dropColumn('created_at'); 
      $table->dropColumn('updated_at'); 
     }); 

     Schema::table('alternatives', function ($table) { 
      $table->integer('created_at'); 
      $table->integer('updated_at'); 
     }); 
    } 
+0

出於好奇,您使用的是哪個版本的Laravel,以及您使用的是哪個版本的MySQL服務器? –

+0

另外,您是否介意在創建/更改「alternatives」表的位置發佈數據庫遷移? –

+0

我正在使用Laravel 5.2和mysql版本14.14。 – Marco

回答

1

這是當幾個表的修改在一個移民相結合的比較典型的情況。看起來,當你第一次運行遷移時,你在表'alternatives'中創建了索引。但是,在中間的某個地方,腳本失敗了。在這種情況下,Laravel不會自動回滾所有以前執行的命令。這意味着下一次您的遷移在第一條命令上失敗。所以我建議你手動運行回滾,將大的遷移分解爲每個表專用的小遷移。

+0

我找到了一個「解決方法算法」。假設我的遷移文件有三個修改:'a','b','c'。然後我的遷移'up()'在'b'上失敗,意味着'a'成功。我接下來做的是用'down()'的一部分替換'up()',負責恢復'a'並運行'php artisan migrate'。接下來,我從'down()'中刪除所有內容並運行'php artisan migrate --step = 1'。這樣我就回到了我在失敗的遷移之前開始的地方。 – dbr