[英]Laravel migration throw database query exception error
I'm trying to write a laravel database migration with foreign relation. 我正在尝试编写具有外部关系的laravel数据库迁移。 during database migration throwing query exception error.
在数据库迁移期间抛出查询异常错误。
I tired to migrate tables using laravel rules but during migration it showing the unexpected error. 我厌倦了使用laravel规则迁移表,但是在迁移过程中却显示了意外错误。
Users Table 用户表
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();
});
Roles Table 角色表
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();
});
Illuminate\\Database\\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
jt_users
add constraintusers_role_id_foreign
foreign key (role_id
) referencesjt_roles
(id
) on delete cascade)Illuminate \\ Database \\ QueryException:SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:更改表
jt_users
添加约束users_role_id_foreign
外键(role_id
)在删除级联上引用jt_roles
(id
)
You can not add a foreign key to a table which doesn't exist. 您不能将外键添加到不存在的表中。 In your case, you are trying to create a
role_id
before the roles
table is created. 就您而言,您正在尝试在创建
roles
表之前创建一个role_id
。
Within your roles
table migration, you will need to update the users
table once the roles
table has been created: 在
roles
表迁移中,一旦创建了roles
表,您将需要更新users
表:
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');
});
Note: Use Schema::table()
instead of Schema::create()
when making changes to a table. 注意:对表进行更改时, 请使用
Schema::table()
而不是Schema::create()
。
On the down()
method of the roles
migration, you need to drop the foreign key and field: 在
roles
迁移的down()
方法上,您需要删除外键和字段:
Schema::table('users', function (Blueprint $table) {
$table->dropForeign(['role_id']);
$table->dropColumn('role_id');
});
首先,必须迁移具有“主键”(用户)的表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.