Laravel 5 migration identifier name too long

PhpLaravelEloquentLaravel 5

Php Problem Overview


I am trying to run the following migration:

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');
	});
}

But I get the following 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

Php Solutions


Solution 1 - Php

You can pass a custom index name as the second parameter into the foreign() method. Or just use shorter table/column names.

So you want to do something like:

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');
    });
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongeoffs3310View Question on Stackoverflow
Solution 1 - PhpPawel BieszczadView Answer on Stackoverflow