1
votes

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');
    });
3
Noticed the ";;" at the end of your foreign key line? - Duikboot
it's just a typing mistake @Duikboot - Oussema Chaabouni

3 Answers

3
votes

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

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

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.

0
votes

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