PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] how can i fix this eror in laravel" 1005 Can't create table `englishcollage`.`role_user` (errno: 150 "Foreign key constraint is incorrectly formed"

 November 16, 2022     laravel, laravel-6, mysql, php     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing