我已將遷移文件添加到數據庫中現有列的索引。這是我的移民文件:Laravel - 遷移(向表中添加索引會引發錯誤)
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版本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');
});
}
出於好奇,您使用的是哪個版本的Laravel,以及您使用的是哪個版本的MySQL服務器? –
另外,您是否介意在創建/更改「alternatives」表的位置發佈數據庫遷移? –
我正在使用Laravel 5.2和mysql版本14.14。 – Marco