fix: Unique constraint should account for soft deleted records (#11048)
FIXES FRMW-2878 **What** Currently, the `one-to-one` unique constraints does not account for deleted record. This prevents from inserting a new record wth the same fk if another one is deleted. **Caveat** `hasOne` with FK option is meant to be a special case, for example a many to one - one to many without defining the other side of the relation. In that case we don't handle this behaviour and keep it as it is
This commit is contained in:
+2
-11
@@ -228,21 +228,12 @@
|
||||
"schema": "public",
|
||||
"indexes": [
|
||||
{
|
||||
"columnNames": [
|
||||
"address_id"
|
||||
],
|
||||
"composite": false,
|
||||
"keyName": "stock_location_address_id_unique",
|
||||
"primary": false,
|
||||
"unique": true
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_stock_location_address_id",
|
||||
"keyName": "IDX_stock_location_address_id_unique",
|
||||
"columnNames": [],
|
||||
"composite": false,
|
||||
"primary": false,
|
||||
"unique": false,
|
||||
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_id\" ON \"stock_location\" (address_id) WHERE deleted_at IS NULL"
|
||||
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_stock_location_address_id_unique\" ON \"stock_location\" (address_id) WHERE deleted_at IS NULL"
|
||||
},
|
||||
{
|
||||
"keyName": "IDX_stock_location_deleted_at",
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
export class Migration20250120110820 extends Migration {
|
||||
|
||||
async up(): Promise<void> {
|
||||
this.addSql('alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_unique";');
|
||||
this.addSql('drop index if exists "IDX_stock_location_address_id";');
|
||||
this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS "IDX_stock_location_address_id_unique" ON "stock_location" (address_id) WHERE deleted_at IS NULL;');
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql('drop index if exists "IDX_stock_location_address_id_unique";');
|
||||
this.addSql('alter table if exists "stock_location" add constraint "stock_location_address_id_unique" unique ("address_id");');
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS "IDX_stock_location_address_id" ON "stock_location" (address_id) WHERE deleted_at IS NULL;');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user