From da938c1fcf0ecdcdc4cd401f509bf305495973f8 Mon Sep 17 00:00:00 2001 From: jerick Date: Tue, 21 Apr 2026 16:18:25 -0400 Subject: [PATCH] Fix account delete blocked by CsvUpload FK constraint Add onDelete: Cascade to CsvUpload.accountId so deleting an account cascades to its upload records. Also explicitly delete uploads before the account in the API route so existing deployed DBs without the constraint don't error. Co-Authored-By: Claude Sonnet 4.6 --- prisma/schema.prisma | 2 +- src/app/api/accounts/[id]/route.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2384edb..485d393 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -103,7 +103,7 @@ model BudgetRule { model CsvUpload { id String @id @default(cuid()) accountId String - account Account @relation(fields: [accountId], references: [id]) + account Account @relation(fields: [accountId], references: [id], onDelete: Cascade) fileName String rowCount Int importedCount Int diff --git a/src/app/api/accounts/[id]/route.ts b/src/app/api/accounts/[id]/route.ts index 97383fa..502b203 100644 --- a/src/app/api/accounts/[id]/route.ts +++ b/src/app/api/accounts/[id]/route.ts @@ -58,6 +58,7 @@ export async function DELETE(_req: Request, { params }: Params) { }) if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 }) + await prisma.csvUpload.deleteMany({ where: { accountId: id } }) await prisma.account.delete({ where: { id } }) return new NextResponse(null, { status: 204 }) }