Php Laravel迁移引发数据库查询异常错误

Php Laravel迁移引发数据库查询异常错误,php,laravel,migration,Php,Laravel,Migration,我正在尝试编写一个具有外部关系的laravel数据库迁移。数据库迁移期间引发查询异常错误 我厌倦了使用laravel规则迁移表,但在迁移过程中,它显示了意外错误 用户表 Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 150); $table->string('

我正在尝试编写一个具有外部关系的laravel数据库迁移。数据库迁移期间引发查询异常错误

我厌倦了使用laravel规则迁移表,但在迁移过程中,它显示了意外错误

用户表

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 150);
        $table->string('email', 150)->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('phone', 150);
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('role_name',255);
        $table->longText('role_description',255);
        $table->integer('sort_order');
        $table->enum('status',['A','I','D'])->comment('A-active','I-inactive,D-delete'); 
        $table->enum('is_deleted',['Y','N'])->comment('Y-yes,N-no');
        $table->timestamps();
        $table->bigInteger('created_by');
        $table->bigInteger('updated_by')->default(0);
        $table->bigInteger('deleted_by')->default(0);
        $table->timestamp('deleted_at')->nullable();
    });
角色表

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 150);
        $table->string('email', 150)->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('phone', 150);
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('role_name',255);
        $table->longText('role_description',255);
        $table->integer('sort_order');
        $table->enum('status',['A','I','D'])->comment('A-active','I-inactive,D-delete'); 
        $table->enum('is_deleted',['Y','N'])->comment('Y-yes,N-no');
        $table->timestamps();
        $table->bigInteger('created_by');
        $table->bigInteger('updated_by')->default(0);
        $table->bigInteger('deleted_by')->default(0);
        $table->timestamp('deleted_at')->nullable();
    });
Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误: 1215无法添加外键约束(SQL:alter table
jt_users
添加约束
users\u role\u id\u foreign
外键(
role\u id
) 删除级联上的引用
jt_角色
id


不能将外键添加到不存在的表中。在本例中,您正在尝试在创建
角色
表之前创建
角色_id

角色
表迁移中,创建
角色
表后,您需要更新
用户
表:

Schema::create('roles', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('role_name',255);
    $table->longText('role_description',255);
    $table->integer('sort_order');
    $table->enum('status',['A','I','D'])->comment('A-active','I-inactive,D-delete'); 
    $table->enum('is_deleted',['Y','N'])->comment('Y-yes,N-no');
    $table->timestamps();
    $table->bigInteger('created_by');
    $table->bigInteger('updated_by')->default(0);
    $table->bigInteger('deleted_by')->default(0);
    $table->timestamp('deleted_at')->nullable();
});

Schema::table('users', function (Blueprint $table) {
    $table->unsignedBigInteger('role_id');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
注意:对表进行更改时,请使用
Schema::table()
而不是
Schema::create()

roles
迁移的
down()
方法中,您需要删除外键和字段:

Schema::table('users', function (Blueprint $table) {
    $table->dropForeign(['role_id']);
    $table->dropColumn('role_id');
});

首先,必须迁移具有“主键”(users)的表

在用户迁移中,您似乎没有为
角色\u id
定义列定义。迁移顺序也很重要。。。您必须确保首先运行角色迁移。