Laravel 7 一般错误: 1005 无法创建表"edmart".cancelled_ex ps' (errno: 121 "Duplicate key on write or update"



嗨,有人在堆栈上寻找解决方案,无法解决我的问题。 我正在使用拉拉维尔7.x。起初我的迁移工作正常,但是当我将$table->foreignId("exp_id")->constrained("expenses")->index();更改为$table->foreignId("fk_expense")->constrained("expenses")->index();时,它开始在迁移时显示此错误

SQLSTATE[HY000]: General error: 1005 Can't create table `edmart`.`cancelled_ex
ps` (errno: 121 "Duplicate key on write or update") (SQL: alter table `cancelled
_exps` add constraint `1` foreign key (`fk_expense`) references `expenses` (`id`
))

波纹管是费用的迁移文件

public function up(){
Schema::create('expenses', function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->constrained("users")->index();
$table->string("desc");
$table->string("amount");
$table->string("status");
$table->timestamps();
});
}

已取消费用迁移文件

public function up()
{
Schema::create('cancelled_exps', function (Blueprint $table) {
$table->id();
$table->foreignId("fk_expense")->constrained("expenses")->index();
$table->string('viewed');
$table->timestamps();
});
}

我试图删除数据库并创建一个新数据库,但仍然失败了。更新:cancelled_exps外,所有其他迁移都正常工作

我将$table->foreignId("fk_expense")->constrained("expenses")->index()更改为

$table->foreignId("expences_id")->constrained()
$table->index('expense_id');
  1. 我将外键列名称更改为expense_id因此我没有 必须在链接constrained()中指定费用表 方法。
  2. 我还改变了链接索引的方式。

整个迁移文件如下所示。

public function up()
{
Schema::create('cancelled_exps', function (Blueprint $table) {
$table->id();
$table->foreignId("expense_id")->constrained();
$table->index("expense_id");
$table->string('viewed');
$table->timestamps();
});
}

相关内容

最新更新