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();
});
}
}
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.
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?
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.
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
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');
});
}
}
and don't forget to make the foreign keys unsigned.
$table->integer('region_id')->unsigned();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With