From b702528e19938b4327151c95d1d956ebe8281872 Mon Sep 17 00:00:00 2001 From: toanhoang Date: Tue, 4 Aug 2026 06:41:13 -0400 Subject: [PATCH] fix: change Assignment.assetItemId/licenseId FK onDelete to RESTRICT Was implicitly SET NULL (Prisma default for nullable FK columns), inconsistent with employeeId's RESTRICT. Once a delete endpoint for AssetItem/License exists, SET NULL would silently null out historical Assignment rows on delete, erasing what was assigned and re-violating the assetItemId/licenseId XOR retroactively. No delete endpoint exists yet, so no real data was ever affected by the old behavior. Co-Authored-By: Claude Sonnet 5 --- .../migration.sql | 11 +++++++++++ prisma/schema.prisma | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 prisma/migrations/20260804103916_assignment_fk_restrict/migration.sql diff --git a/prisma/migrations/20260804103916_assignment_fk_restrict/migration.sql b/prisma/migrations/20260804103916_assignment_fk_restrict/migration.sql new file mode 100644 index 0000000..2ef27a3 --- /dev/null +++ b/prisma/migrations/20260804103916_assignment_fk_restrict/migration.sql @@ -0,0 +1,11 @@ +-- DropForeignKey +ALTER TABLE "assignments" DROP CONSTRAINT "assignments_asset_item_id_fkey"; + +-- DropForeignKey +ALTER TABLE "assignments" DROP CONSTRAINT "assignments_license_id_fkey"; + +-- AddForeignKey +ALTER TABLE "assignments" ADD CONSTRAINT "assignments_asset_item_id_fkey" FOREIGN KEY ("asset_item_id") REFERENCES "asset_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "assignments" ADD CONSTRAINT "assignments_license_id_fkey" FOREIGN KEY ("license_id") REFERENCES "licenses"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d5b63b5..94aa239 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -398,9 +398,9 @@ model Assignment { employeeId String @map("employee_id") @db.Uuid employee Employee @relation(fields: [employeeId], references: [id]) assetItemId String? @map("asset_item_id") @db.Uuid - assetItem AssetItem? @relation(fields: [assetItemId], references: [id]) + assetItem AssetItem? @relation(fields: [assetItemId], references: [id], onDelete: Restrict) licenseId String? @map("license_id") @db.Uuid - license License? @relation(fields: [licenseId], references: [id]) + license License? @relation(fields: [licenseId], references: [id], onDelete: Restrict) assignedAt DateTime @default(now()) @map("assigned_at") @db.Timestamptz(6) returnedAt DateTime? @map("returned_at") @db.Timestamptz(6) signedAt DateTime? @map("signed_at") @db.Timestamptz(6)