我制作了一个迁移文件,在其中向数据库中的现有列添加索引。这是我的迁移文件:
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 tablealternatives
add indexalternatives _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');
});
}