SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4

SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Foreign Key Constraint Errors in Laravel Migrations

When running php artisan migrate, encountering SQLSTATE[HY000]: General error: 1005 Can't create table followed by errno: 150 is a classic symptom of a foreign key constraint violation. This specific error often appears when the database attempts to create a new table but fails because that table references a column in another table that doesn't exist yet, or where the referenced columns have incompatible definitions.

In the provided scenario, the migration for the gigs table attempts to define a foreign key relationship pointing to the bands table:

$table->foreign('band_id')
    ->references('band_id')->on('bands')
    ->onDelete('cascade');

Simultaneously, the migration tries to establish a relationship with the stages table. However, if the stages migration hasn't been executed yet, or if its schema isn't ready, this operation will fail immediately upon the creation of the gigs table. The MySQL error message specifically highlights the failure during the ALTER TABLE command that adds the constraint, which is executed after the base columns are created but before the table is fully committed.

Execution Order Matters

The most critical factor in resolving this issue is ensuring that tables with no dependencies are migrated first. Laravel migrations execute sequentially based on their timestamp or filename order if they reside in the same directory. If the gigs migration runs before the bands and stages migrations, the target tables simply do not exist when the foreign key constraint is being added.

To fix this without deleting files, you should check your migration filenames. Ensure that 2023_01_01_create_bands_table.php (for example) has a lower timestamp than 2023_01_01_create_gigs_table.php. If the timestamps are identical or if the dependency order is ambiguous, you may need to rename the files to enforce the correct execution sequence.

Schema Consistency and Column Types

Beyond execution order, the column definitions must match perfectly. A foreign key constraint requires that the referenced column in the child table (gigs.band_id) be compatible with the primary key in the parent table (bands.band_id). In the provided migrations, both are defined as integer with unsigned(). While this often works, explicit typing is safer. If the band_id column was created as a signed integer in one migration and an unsigned integer in another, or if the length of a string primary key differs, MySQL will reject the constraint.

It is also vital to verify that the down() methods are logically consistent with the up() methods. The gigs migration drops the foreign keys using specific names (gigs_band_id_foreign, gigs_stage_id_foreign). Ensure these names match exactly what Laravel generates by default, which usually aligns with the column name and table structure, though custom naming can occur if explicitly defined.

Model Relationships vs. Database Constraints

A common misconception is that defining relationships in Eloquent models automatically creates or enforces database constraints. The Band and Gig model classes shown use hasOne and belongsTo to define the logical association. These are purely PHP-level helpers for querying; they do not generate SQL ALTER TABLE statements or enforce data integrity at the database level.

The foreign key constraints are strictly defined within the migration files using the foreign() method. If your models reference tables that haven't been migrated yet, you will get errors when trying to access those relationships, but the root cause remains the missing database constraint setup. The model code is correct for an ORM context, but it cannot fix a missing table in the database schema.

Verifying the Environment

If you are using multiple environments (development, staging, production), ensure you are not mixing migrations from different branches or forgetting to pull changes. Sometimes, a migration file might be missing on a specific server because it wasn't pushed to the code repository. Running php artisan migrate:status can help identify which tables have been created in your current environment versus what the latest version of your code expects.

If you are still facing issues after correcting the order and schema definitions, consider checking for existing data. If rows exist in the gigs table that reference a non-existent band_id, adding a new foreign key constraint will fail due to referential integrity violations. You must either delete or update those records before applying the migration.

Our network