Я создал файл миграции, в котором я добавляю индексы к существующим столбцам в моей базе данных. Это моя миграция файл:Laravel - Migration (добавление индексов в таблицы вызывает ошибку)
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');
});
}
Просто из любопытства, какую версию Laravel вы используете, и какую версию сервера MySQL вы используете? –
Кроме того, не могли бы вы опубликовать миграцию (ы) базы данных, где вы создаете/изменяете таблицу «альтернативы»? –
Я использую Laravel 5.2 и mysql Ver 14.14. – Marco