如何在laravel中修复此错误”,“ 1005无法创建表” englishcollage`。” role_user`(错误号:150“外键约束格式不正确”

时间:2020-03-24 15:45:06

标签: php mysql laravel laravel-6

我想在我的Laravel项目中进行ACl迁移...(版本6)

但是我收到这个烦人的错误:

一般错误:1005无法创建表English Collagerole_user(错误:150“外键约束格式不正确”))

English Collage是我的数据库。

 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');

    });

这是我的用户迁移:

 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();
        });

这种结构在laravel 5中起作用,但是laravel 6对此有疑问

即使我为role_user测试primary_key,我也尝试了大整数和无符号大整数

3 个答案:

答案 0 :(得分:1)

这行很可能是

$table->Integer('role_id')->unsigned();

如果角色表的id字段不是Integer(最新的Laravel使用unsignedBigInteger),则MySQL不会满意。

尝试

$table->unsignedBigInteger('role_id');

答案 1 :(得分:1)

要创建外键,child column的数据类型必须与parent column完全匹配。

由于idbigIncrements,即unsigned big integer表中的users,因此role_user.user_id必须是unsignedbigInteger,而不是unsignedInteger

在您的role_user表中的

更改

 $table->Integer('user_id')->unsigned();

 $table->unsignedBigInteger('user_id');

答案 2 :(得分:0)

尝试一下,我希望它能正常工作

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')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade')->unsigned();

    $table->Integer('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade')->unsigned();

});

Schema::create('permission_role', function (Blueprint $table) {

    $table->unsignedInteger('permission_id')->references('id')->on('permissions')->onDelete('cascade')->onUpdate('cascade');

    $table->unsignedInteger('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');


});