Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Migrations throwing 1072 error

I have a couple of migrations in a new Laravel 4 project. One is for regions and the other for areas. Each region has a number of areas, and areas belong to regions.

I have used Laravel 4 and the migration functions on a number of occasions but have never come accross this issue before. When I run php artisan migrate:install followed by php artisan migrate I get the following error:

$ php artisan migrate
  [Exception]
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'region_
  id' doesn't exist in table (SQL: alter table `areas` add constraint areas_r
  egion_id_foreign foreign key (`region_id`) references `regions` (`id`)) (Bi
  ndings: array (
  ))
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="...
"]] [--pretend] [--seed]

// The regions migration

class CreateRegionsTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the regions table
   Schema::create('regions', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name', 160)->unique();
        $table->timestamps();
    });
  }
}

// The areas migration

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the cemeteries table
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->foreign('region_id')->references('id')->on('regions');
        $table->string('name', 160)->unique();
        $table->timestamps();
    });
  }
}
like image 656
JasonMortonNZ Avatar asked Jun 04 '13 21:06

JasonMortonNZ


People also ask

What does syntax error 1071 mean in Laravel migration?

Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes. You should answer your question in an answer.

Why am I getting this error in Laravel when trying to import emojis?

You will receive this error in laravel 5.4 and above because laravel upgraded to utf8mb4 database character set, which include support for storing emojis.If you are running MySQL 5.7 and higher you will likely not receive this errors. Why error?

How does the Order of migrations work in Laravel?

Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations: Laravel will use the name of the migration to attempt to guess the name of the table and whether or not the migration will be creating a new table.

What is Q&A for work Laravel?

Q&A for work Connect and share knowledge within a single location that is structured and easy to search. Learn more Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Ask Question Asked4 years, 7 months ago Active4 months ago


2 Answers

You have to create the column related to the foreign key:

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the cemeteries table
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');

        $table->integer('region_id')->unsigned();
        $table->foreign('region_id')->references('id')->on('regions');

        $table->string('name', 160)->unique();
        $table->timestamps();

    });
  }
}

Sometimes (depends on your database server) you'll have to create your foreign keys in two steps:

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Create the table and the foreign key column
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');

        $table->integer('region_id')->unsigned();

        $table->string('name', 160)->unique();
        $table->timestamps();

    });

    // Create the relation
    Schema::tabe('areas', function($table)
    {
        $table->foreign('region_id')->references('id')->on('regions');
    });
  }
}
like image 185
Antonio Carlos Ribeiro Avatar answered Oct 25 '22 03:10

Antonio Carlos Ribeiro


and don't forget to make the foreign keys unsigned.

         $table->integer('region_id')->unsigned();
like image 33
Iman Mohamadi Avatar answered Oct 25 '22 05:10

Iman Mohamadi