【发布时间】:2021-03-06 05:55:38
【问题描述】:
我在 Postgres 7.2 中遇到此错误:
无效的外键:7 错误:没有唯一约束匹配给定键的引用表“holiday_items”(SQL:更改表“holiday_bookings”添加约束“holiday_items_parent_holiday_item_id_foreign”外键(“parent_holiday_item_id”)引用“holiday_items”(“ id") 删除级联)
这是我正在尝试运行的迁移:
Schema::create('holiday_bookings', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('booking_id')->index();
$table->integer('booking_version')->index();
$table->uuid('parent_holiday_item_id')->nullable();
$table->unique(["id", "booking_id", "booking_version"]);
$table->foreign('parent_holiday_item_id')->nullable()->references('id')->on('holiday_items')->onDelete('cascade');
$table->foreign('quotation_id')->references('id')->on('quotations')->onDelete('cascade');
$table->timestamps();
});
下面是 holiday_items 表中带有 unqiue 约束的字段:
Schema::create('holiday_items', function (Blueprint $table) {
$table->uuid('id');
$table->integer('booking_version')->index();
$table->uuid('booking_id')->index();
$table->timestamps();
$table->unique(["id", "booking_id", "booking_version"]);
$table->foreign(['booking_version', 'booking_id'])->references([
'booking_version',
'id'
])->on('bookings')->onDelete('cascade');
});
我在 holiday_bookings 迁移中分配的外键有什么问题?
【问题讨论】:
-
您是否尝试过对 parent_holiday_item_id 字段使用 onUpdate('cascade')?
标签: laravel postgresql migration