Oussema Chaabouni
Oussema Chaabouni

Reputation: 689

General error: 1005 Can't create table

When i run php artisan migrate i keep getting this error:

[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 Can't create table binomi.#sql-3910_c0b (errno: 150 "Foreign key constraint is incorrectly formed")

Here is the schema for my user and activity model and those are the only models in the app.

User migration:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('activity');
        $table->rememberToken();
        $table->timestamps();
    });

  Schema::table('users', function($table){
      $table->foreign('activity')->references('id')->on('activity')->onDelete('cascade');
     }

Activity migration:

 Schema::create('activty', function (Blueprint $table) {
        $table->increments('id');
        $table->string('label');
    });

Upvotes: 1

Views: 3385

Answers (3)

ishadif
ishadif

Reputation: 731

The increments() automatically assigned as unsigned.

So, make sure you add unsigned as in the code below:

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

and also check whether you make a typo or not

Upvotes: 0

Angad Dubey
Angad Dubey

Reputation: 5452

Add the unsigned() modifier to your foreign key column:

Schema::create('users', function (Blueprint $table) {
        ...
        $table->integer('activity')->unsigned();
        $table->rememberToken();
        $table->timestamps();
    });

Upvotes: 4

Joel Hinz
Joel Hinz

Reputation: 25414

Your activities table has a spelling error, it says activty in your schema creation. Also, I would suggest renaming it activities unless you only have one.

Upvotes: 1

Related Questions