我已经创建了一个迁移文件,我将索引添加到数据库中的现有列。这是我的迁移文件:
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
时出现错误:
[照亮\数据库\ QueryException]
SQLSTATE [42000]:语法错误或访问冲突:1061重复键 name'alternative_question_id_index'(SQL:alter tablealternatives
添加索引alternatives _question_id_index
(question_id
))[PDOException]
SQLSTATE [42000]:语法错误或访问冲突:1061重复键 名称'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');
});
}
答案 0 :(得分:1)
在一次迁移中组合多个表的修改时,这是一种非常典型的情况。看来,当您第一次运行迁移时,您在表'alternative'中创建了索引。但是,在中间某处您的脚本失败了。在这种情况下,Laravel不会自动回滚所有先前执行的命令。这意味着下次您的迁移在第一个命令上失败。因此,我建议您手动运行回滚,将大型迁移分成专用于每个表的小型迁移。