我制作了一个迁移文件,在其中向数据库中的现有列添加索引。这是我的迁移文件:
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 tablealternatives
add indexalternatives _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 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');
});
}
最佳答案
这是一种非常典型的情况,即在一次迁移中合并多个表的修改。似乎当您第一次运行迁移时,您在表“alternatives”中创建了索引。然而,在中间的某个地方你的脚本失败了。在这种情况下,Laravel 不会自动回滚所有先前执行的命令。这意味着下次您的迁移在第一个命令上会失败。因此,我建议您手动运行回滚,将大迁移拆分为每个表专用的小迁移。
关于php - Laravel - 迁移(向表添加索引会引发错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39228672/