El nombre del identificador de migración de Laravel 5 es demasiado largo

2 minutos de lectura

Estoy tratando de ejecutar la siguiente migración:

public function up()
{
    Schema::create('lifestyle_questions', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('question');
        $table->timestamps();
    });

    Schema::create('lifestyle_question_answers', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('lifestyle_question_id')->unsigned();
        $table->foreign('lifestyle_question_id')->references('id')->on('lifestyle_questions');
        $table->string('answer');
        $table->timestamps();
    });

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table)
    {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('lifestyle_question_answer_id')->unsigned();
        $table->foreign('lifestyle_question_answer_id')->references('id')->on('lifestyle_question_answers');
    });
}

Pero me sale el siguiente error:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long (SQL: alter table `user_lifestyle_question_answers` add constraint user_lifestyle_question_answers_lifestyle_question_answer_id_foreign foreign key (`lifestyle_question_answer_id`) references `lifestyle_question_answers` (`id`))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long

avatar de usuario
Pawel Bieszczad

Puede pasar un nombre de índice personalizado como segundo parámetro en el método Foreign(). O simplemente use nombres de tabla/columna más cortos.

Así que quieres hacer algo como:

public function up()
{
    Schema::create('lifestyle_questions', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('question');
        $table->timestamps();
    });

    Schema::create('lifestyle_question_answers', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('lifestyle_question_id')->unsigned();
        $table->foreign('lifestyle_question_id', 'lq_id_foreign')->references('id')->on('lifestyle_questions');
        $table->string('answer');
        $table->timestamps();
    });

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table)
    {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('lifestyle_question_answer_id')->unsigned();
        $table->foreign('lifestyle_question_answer_id', 'lqa_id_foreign')->references('id')->on('lifestyle_question_answers');
    });
}

  • Solo para información: los identificadores de MySQL siempre deben tener menos de 64 caracteres según dev.mysql.com/doc/refman/5.5/en/identificadores.html

    – Pᴇʜ

    11 mayo 2015 a las 14:37

  • Desafortunadamente, esta solución no funciona con el primary() método al generar claves compuestas: $table->primary(['id1', 'id'], 'custom_index'); ¿Cómo resolver este problema?

    – Dong3000

    8 de mayo de 2018 a las 13:04

  • Para aquellos que tienen este problema al generar claves compuestas como @Dong3000, vean esto: stackoverflow.com/a/28626907/3368784

    – Juana

    16 mayo 2019 a las 18:40


  • Para las personas que usan foreignId puedes usarlo así: $table->foreignId('really_long_foreign_key_id')->constrained()->index('short_id_foreign');

    – Fredrik Jungstedt

    12 de junio de 2020 a las 13:11

¿Ha sido útil esta solución?