Issue
i want to make a ACl migration in my Laravel project ... (version 6)
but i receive this annoying error :
General error: 1005 Can't create table English Collage
.role_user
(Errno: 150 "Foreign key constraint is incorrectly formed")")
English Collage is my database .
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('title_fa'); // edit posts
$table->string('title_en'); //edit-posts
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('title_fa'); // edit posts
$table->string('title_en'); //edit-posts
$table->timestamps();
});
Schema::create('role_user', function (Blueprint $table) {
$table->Integer('role_id')->unsigned();
$table->Integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedInteger('permission_id');
$table->unsignedInteger('role_id');
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
});
and this is my users migration:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('image')->nullable();
$table->string('level')->nullable();
$table->integer('age')->nullable();
$table->rememberToken();
$table->timestamps();
});
this structure work in laravel 5 but laravel 6 has issue with this
i tried big integer and unsigned big integer even i tested primary_key for role_user
Solution
To create Foreign key the data type for the child column
must match the parent column
exactly.
Since id
is a bigIncrements
i.e. unsigned big integer
in users
table then role_user.user_id
must be unsignedbigInteger
, not an unsignedInteger
.
Change below in your role_user
table
$table->Integer('user_id')->unsigned();
to
$table->unsignedBigInteger('user_id');
Answered By - Sehdev Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.