1

我制作了一个迁移文件,在其中向数据库中的现有列添加索引。这是我的迁移文件:

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]:语法错误或访问冲突:1061 Duplicate key name 'alternatives_question_id_index'(SQL:alter table alternatives add index alternatives _question_id_index( question_id))

[PDOException]
SQLSTATE[42000]:语法错误或访问冲突:1061 Duplicate key name 'alternatives_question_id_index'

这很奇怪,因为我没有替代表的现有索引,即使我删除了该行,我也会得到该列的相同错误,该'correct'列也没有以前的任何索引。对于表中的所有列,我也得到相同的错误'answers'

我正在使用 Laravel 5.2 和 mysql Ver 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');
        });
    }
4

1 回答 1

2

当多个表的修改合并在一个迁移中时,这是一种非常典型的情况。似乎当您第一次运行迁移时,您在表“替代”中创建了索引。但是,在中间的某个地方,您的脚本失败了。在这种情况下,Laravel 不会自动回滚之前执行的所有命令。这意味着下次您的迁移在第一个命令上失败。所以我建议您手动运行回滚,将大迁移拆分为一个专用于每个表的小迁移。

于 2016-08-30T13:32:24.907 回答