2

I have made a migration file where I am adding indexes to existing columns in my database. This is my migration file:

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');
        });
    }

But when I run php artisan migrate I get an error:

[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'

Which is weird since I don't have an already existing index for alternatives table and even when I delete that line, I get the same error for the 'correct' column as well, which also doesn't have any index from before. I get the same error for all the columns in 'answers' table too.

I am using Laravel 5.2 and mysql Ver 14.14.

And this is how my migration file for creating 'alternatives' table looks like:

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');
        });
    }
Ludwig
  • 1,401
  • 13
  • 62
  • 125
  • Just out of curiousity, which version of Laravel are you using, and what version of the MySQL server are you using? – Chris Forrence Aug 30 '16 at 13:31
  • Also, would you mind posting your database migration(s) where you create/alter the "alternatives" table? – Chris Forrence Aug 30 '16 at 13:32
  • I am using Laravel 5.2 and mysql Ver 14.14. – Ludwig Aug 30 '16 at 13:39
  • Alright, so by "I don't have an already existing index for alternatives table", can you verify that? `composer require doctrine/dbal`, then `php artisan tinker`, then `Schema::getConnection()->getDoctrineSchemaManager()->listTableIndexes('alternatives');` – Chris Forrence Aug 30 '16 at 13:52

1 Answers1

3

This is a quite typical situation when modifications of several tables combined in one migration. It seems that when you ran the migration first time you created indexes in the table 'alternatives'. However, somewhere in the middle your script failed. In this case Laravel doesn't make auto-rollback of all previous executed commands. It means that next time your migration fails on the first command. So I suggest you to run rollback manually, split big migration in small one dedicated for each table.

Andrej
  • 7,474
  • 1
  • 19
  • 21
  • I found a "workaround algorithm" for this behavior. Lets say my migration file runs three modifications: `a`, `b`, `c`. Then my migration `up()` fails on `b` meaning `a` was successful. What I do next is replace `up()` with part of `down()` responsible for reverting `a` and run `php artisan migrate`. Next i delete everything from `down()` and run `php artisan migrate --step=1`. This way I'm back where I started before the failed migration. – dbr Jun 06 '17 at 01:13