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

Related searches

I get this error when I do php artisan migrate. Is there something wrong in my migration files? Or is it possible my models are wrong coded? But the migrations should work even there is something wrong in the models?

[Exception]                                                                  
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150) (SQL: alter table `gigs` add constraint gigs_band_  
  id_foreign foreign key (`band_id`) references `bands` (`band_id`) on delete  
   cascade) (Bindings: array (                                                 
  ))

[PDOException]                                                               
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150)

gigs migration

public function up()
    {
        Schema::create('gigs', function($table)
        {
            $table->increments('gig_id');

            $table->dateTime('gig_startdate');

            $table->integer('band_id')->unsigned();
            $table->integer('stage_id')->unsigned();

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

            $table->foreign('stage_id')
                ->references('stage_id')->on('stages')
                ->onDelete('cascade');
        });

    public function down()
    {
        Schema::table('gigs', function($table)
        {
            Schema::drop('gigs');
            $table->dropForeign('gigs_band_id_foreign');
            $table->dropForeign('gigs_stage_id_foreign');
        });
    }

bands migration

public function up()
    {
        Schema::create('bands', function($table)
        {
            $table->increments('band_id');

            $table->string('band_name');
            $table->text('band_members');
            $table->string('band_genre');
            $table->dateTime('band_startdate');
        });
    }

    public function down()
    {
        Schema::table('bands', function(Blueprint $table)
        {
            Schema::drop('bands');
        });
    }

Model Band

<?php

class Band extends Eloquent {

    protected $primaryKey = 'band_id';

    public function gig()
    {
        return $this->hasOne('Gig', 'band_id', 'band_id');
    }
}

Model Gig

<?php

class Gig extends Eloquent {
    protected $primaryKey = 'gig_id';

    public function gig()
    {
        return $this->belongsTo('Band', 'band_id', 'band_id');
    }

    public function stage()
    {
        return $this->belongsTo('Stage', 'stage_id', 'stage_id');
    }
}

You must first create the table, then create the foreign keys:

Schema::create('gigs', function($table)
{
    $table->increments('gig_id');

    $table->dateTime('gig_startdate');

    $table->integer('band_id')->unsigned();
    $table->integer('stage_id')->unsigned();
});

Schema::table('gigs', function($table)
{
    $table->foreign('band_id')
        ->references('band_id')->on('bands')
        ->onDelete('cascade');

    $table->foreign('stage_id')
        ->references('stage_id')->on('stages')
        ->onDelete('cascade');
});

And your bands table should migrate first, since the gigs is referencing it.

SQLSTATE[HY000]: General error: 1005 Can't create table, You must first create the table, then create the foreign keys: Schema::create('gigs' , function($table) { $table->increments('gig_id');� [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table `pgaiiis`.`#sql-8e8_1ab` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `specimen_types` add constraint `spe cimen_types_section_id_foreign` foreign key (`section_id`) references `sections` (`id`)) [PDOException] SQLSTATE


While this doesn't apply to OP, others might have this issue:

From the bottom of the Laravel Schema docs:

Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.

You can do this via $table->integer('user_id')->unsigned(); when creating your table in the migration file.

Took me a few minutes to realize what was happening.

"General error: 1005 Can't create table" Using Laravel Schema Build , SQLSTATE[HY000]: General error: 1005 Can't create table 'nabsh.#sql-5b0_e' ( errno: 121) (SQL: alter table usermeta add constraint� SQLSTATE[HY000]: General error: 1005 Can't create table Posted 2 years ago by jashnkrish Please sign in or create an account to participate in this conversation.


For those whom other answers doesn't help, the same error throws also when you try to use 'SET_NULL' action on non-nullable column.

SQLSTATE[HY000]: General error: 1005 Can't create table `vanila , Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table vanila . #sql-1d4a_1e (errno: 150 "Foreign� Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table vanila.#sql-1d4a_1e (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table profiles add constraint profiles_user_id_foreign foreign key (user_id) references users (id))


As said by Andrew by making the reference on the table as this:

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

It should work.

SQLSTATE[HY000]: General error: 1005 Can't create table 'bayvogue_magento.catalog_product_index_eav_idx' (errno: -1) Processed 100% 13/13 records Imported 0 records


for those who are still having this issue, make sure the field you setting as foreign key is a primary key or should be unique since a foreign key can only be a primary or unique field. see sample below

Schema::create('users', function (Blueprint $table) {
  $table->increments('id');
  $table->string('username',25)->unique();
 });

Schema::create('another_table', function (Blueprint $table) {
   $table->increments('id');
   $table->string('name', 25);
   $table->foreign('name')->references('username')->on('users')
 });

Instead of referring to an inability to create a table with an auto-generated name, the message clearly states the error: ERROR 1215 (HY000): Cannot add foreign key constraint Issuing a subsequent SHOW WARNINGS statement provides additional detail about any secondary indexes that are required.


Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Ca n’t create table `dbstudent`.`#sql-11c8_1f` (errno: 150 “Foreign key constraint is incorrectly formed”) (SQL: alter table `transactions` add constraint `transac


[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table binomi.#sql-3910_c0b (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint users_activity_foreign foreign key (activity) references activity (id) on delete cascade) [PDOException] SQLSTATE[HY000]: General


ERROR 1005 (HY000): Can't create table 'tracker_phonetracker.location_share' (errno: -1) SQLSTATE[HY000]: General error: 1030 General error: 1005 Can't create table ,Foreign key constraint is incorrectly formed in laravel