Support the ongoing development of Laravel.io →
Database Eloquent

I am currently receiving the following error when I run my migrations:

SQLSTATE[HY000]: General error: 1005 Can't create table 'saferides.#sql-189_4bc' (errno: 150) (SQL: alter table `rides` add constraint rides_car_id_foreign foreign key (`car_id`) references `car` (`id`) on delete cascade)

After reading a lot of issues about errors of this nature in Laravel, I have done the following to avoid it:

  • Not reference a primary key with a foreign key until the primary key is created ( I make sure of this by keeping my table creations and foreign key assignments in separate migrations in the following order creation -> keys
  • Make sure the primary key and the foreign key are of the same type (unsigned int(10))
  • Explicitly state the storage engine to be InnoDB

Currently, I have my migrations set up so that the tables are created first:

// Up function for cars table
public function up()
{
	Schema::create('cars', function(Blueprint $table)
	{
		// Explicitly state storage engine
		$table->engine = 'InnoDB';

		// Primary Key
		$table->increments('id')->unsigned();

		// Other columns
		$table->string('car_num');
		$table->string('available_seats');
		
		// Indexes to foreign keys
		$table->integer('user_id')->unsigned()->index();
		$table->integer('ride_id')->unsigned()->index();
	});
}

// Up function for rides table
public function up()
{
	Schema::create('rides', function(Blueprint $table)
	{
		// Explicitly state storage engine
		$table->engine = 'InnoDB';

		// Primary Key
		$table->increments('id')->unsigned();

		// Other columns
		$table->boolean('completed')->default(0);
		$table->boolean('no_show')->default(0);
		
		// Indexes to foreign keys
		$table->integer('location_id')->unsigned()->index();
		$table->integer('user_id')->unsigned()->index();
		$table->integer('car_id')->unsigned()->index();
	});
}

After all my tables are created, I add my foreign keys to the tables.

public function up()
{
	Schema::table('cars', function(Blueprint $table) 
	{
		$table->foreign('user_id')
			  ->references('id')->on('users')
			  ->onDelete('cascade');
			  
		$table->foreign('ride_id')
			  ->references('id')->on('rides')
			  ->onDelete('cascade');
	});
}

public function up()
{
	Schema::table('rides', function(Blueprint $table) 
	{
		$table->foreign('location_id')
			  ->references('id')->on('locations')
			  ->onDelete('cascade');

		$table->foreign('user_id')
			  ->references('id')->on('users')
			  ->onDelete('cascade');

		$table->foreign('car_id')
			  ->references('id')->on('car')
			  ->onDelete('cascade');
	});
}

Any advice on how to fix this would be appreciated.

Last updated 3 years ago.
0

The table you reference in the last foreign key statement is 'car' but the table is 'cars'.

Does this help?

Last updated 3 years ago.
0

This issue hasn't even been fixed in 5.1. What can we do to have Tyler fix this in the next release?

0

As I see, this is not a laravel issue, this is a MySQL issue.

IMHO, is not a bug, its a detail of implementation: you cannot reference a FK to a yet-not-created table and table PK.

The way around that is simple: order your migrations to guarantee that all FK declarations references pre-created tables.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

erneyja erneyja Joined 13 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.