:1005无法创建表`shop`.`role_user`(错误号:150“外键约束格式不正确”)))

时间:2020-04-04 21:39:16

标签: php mysql laravel

伙计们,我尝试了很多东西,读了一些博客或讨论,但是我没有解决我的问题,我是laravel这个项目的新手。我想创建数据库错误时遇到错误

E:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
      PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `shop`.`role_user` (errno: 150 "Foreign key constraint is incorrectly formed")")

  2   E:\xampp\htdocs\shop\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
      PDOStatement::execute()

class CreateRoleAndPermissionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('title')->nullable();
            $table->timestamps();
        });
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('title')->nullable();
            $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');

            $table->primary(['role_id','user_id']);
        });

        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('role_id')->unsigned();
            $table->integer('permission_id')->unsigned();


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

            $table->foreign('permission_id')
                ->references('id')
                ->on('permissions')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table->primary(['permission_id','role_id']);
        });


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('role_and_permission');
    }

1 个答案:

答案 0 :(得分:0)

integer的所有实例更改为biginteger,我假设您使用的是Laravel的当前版本,该版本将增量字段更改为“ biginteger”。还有$table->increments('id');$table->id();