diff --git a/apps/api/src/trigger/policies/update-policy-helpers.ts b/apps/api/src/trigger/policies/update-policy-helpers.ts index dbf7c5595..0739ad3e3 100644 --- a/apps/api/src/trigger/policies/update-policy-helpers.ts +++ b/apps/api/src/trigger/policies/update-policy-helpers.ts @@ -251,7 +251,12 @@ export async function updatePolicyInDatabase( } await db.$transaction(async (tx) => { + // Clear version references first to avoid FK constraint issues during deletion if (policy.versions.length > 0) { + await tx.policy.update({ + where: { id: policyId }, + data: { currentVersionId: null, pendingVersionId: null }, + }); await tx.policyVersion.deleteMany({ where: { policyId } }); } diff --git a/apps/app/src/trigger/tasks/onboarding/update-policies-helpers.ts b/apps/app/src/trigger/tasks/onboarding/update-policies-helpers.ts index 31582ab53..899b02c07 100644 --- a/apps/app/src/trigger/tasks/onboarding/update-policies-helpers.ts +++ b/apps/app/src/trigger/tasks/onboarding/update-policies-helpers.ts @@ -561,8 +561,12 @@ export async function updatePolicyInDatabase( // Use transaction to ensure atomicity - if any step fails, all are rolled back await db.$transaction(async (tx) => { - // Delete all existing versions + // Clear version references first to avoid FK constraint issues during deletion if (policy.versions.length > 0) { + await tx.policy.update({ + where: { id: policyId }, + data: { currentVersionId: null, pendingVersionId: null }, + }); await tx.policyVersion.deleteMany({ where: { policyId }, });