jeudi 7 septembre 2017

(Laravel) errno: 150 "Foreign key constraint is incorrectly formed

I try to migrate my tables but it display an error which is: errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint users_role_id_foreign foreign key (role_id) references roles (id))

This is my tables:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('fullname');
        $table->string('username')->unique();
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('role_id')->unsigned();
        $table->foreign('role_id')->references('id')->on('roles');
        $table->rememberToken();
        $table->timestamps();
    });


Schema::create('roles', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('name')->unique();
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });


Schema::create('posts', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('content');
        $table->string('image');
        $table->timestamps();
    });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire