Skip to content

Investigation: Partner deletion cascade error with Prisma + PlanetScale#3282

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/investigate-error-sources
Closed

Investigation: Partner deletion cascade error with Prisma + PlanetScale#3282
Copilot wants to merge 1 commit intomainfrom
copilot/investigate-error-sources

Conversation

Copy link

Copilot AI commented Dec 26, 2025

Investigation Summary

This PR contains an investigation into a PrismaClientKnownRequestError that started occurring around December 22nd when attempting to delete Partner records. The error indicates: Unknown column 'ProgramEnrollment.programId' in 'field list'.

Root Cause Identified

The error is caused by Prisma's cascade delete handling with PlanetScale's relationMode = "prisma" configuration:

  1. The schema uses relationMode = "prisma" (required for PlanetScale compatibility)
  2. This means Prisma handles referential integrity at the application level, not at the database level
  3. When deleting Partner records, Prisma attempts to cascade delete related ProgramEnrollment records but fails because it cannot properly reference ProgramEnrollment.programId in the deletion context

Problematic Code Identified

Two files were recently added (commit 51f4865) that rely on automatic cascade deletes instead of manual cleanup:

  1. apps/web/lib/api/partners/bulk-delete-partners.ts

    • Lines 129-136: Calls partner.deleteMany() directly when deletePartners=true
    • Skips manual deletion of ProgramEnrollment records
  2. apps/web/app/(ee)/api/admin/delete-partner-account/route.ts

    • Line 109: Calls partner.delete() directly
    • Relies on cascade deletes for cleanup

Recommended Fix

Both files need to manually delete ProgramEnrollment records (and potentially other relations from the 30+ models that reference partnerId) BEFORE deleting the Partner record itself, since PlanetScale doesn't support database-level foreign key constraints.

Analysis Details

  • Identified 30+ models with partnerId fields that have cascading relationships
  • Confirmed the schema properly defines onDelete: Cascade relations
  • Issue is specific to PlanetScale's lack of database-level foreign key support
Original prompt

based on our commits from the last 7 days, what could potentially be causing this error?


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link
Contributor

vercel bot commented Dec 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
dub Ready Ready Preview Dec 26, 2025 1:28am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 26, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@steven-tey
Copy link
Collaborator

@copilot based on our commits from the last 7 days, what could potentially be causing this error?

PrismaClientKnownRequestError:
Invalid prisma.partner.deleteMany() invocation in
/Users/steventey/Documents/Dub/dub/apps/web/scripts/find-link.ts:13:37

10 },
11 });
12 console.log(res);
→ 13 const res2 = await prisma.partner.deleteMany(
The column (not available) does not exist in the current database.
at ei.handleRequestError (/Users/steventey/Documents/Dub/dub/node_modules/.pnpm/@prisma+client@6.19.1_prisma@6.19.1_typescript@5.2.2__typescript@5.2.2/node_modules/@prisma/client/src/runtime/RequestHandler.ts:228:13)
at ei.handleAndLogRequestError (/Users/steventey/Documents/Dub/dub/node_modules/.pnpm/@prisma+client@6.19.1_prisma@6.19.1_typescript@5.2.2__typescript@5.2.2/node_modules/@prisma/client/src/runtime/RequestHandler.ts:174:12)
at ei.request (/Users/steventey/Documents/Dub/dub/node_modules/.pnpm/@prisma+client@6.19.1_prisma@6.19.1_typescript@5.2.2__typescript@5.2.2/node_modules/@prisma/client/src/runtime/RequestHandler.ts:143:12)
at async a (/Users/steventey/Documents/Dub/dub/node_modules/.pnpm/@prisma+client@6.19.1_prisma@6.19.1_typescript@5.2.2__typescript@5.2.2/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:833:24)
at async main (/Users/steventey/Documents/Dub/dub/apps/web/scripts/find-link.ts:13:16) {
code: 'P2022',
meta: { modelName: 'Partner', column: '(not available)' },
clientVersion: '6.19.1'
}

This could be a bug with the Prisma adapter + Planetscale when deleting cascaded rows
originalMessage: "target: dub.-.primary: vttablet: rpc error: code = NotFound desc = Unknown column 'ProgramEnrollment.programId' in 'field list'",

Copy link
Author

Copilot AI commented Dec 26, 2025

@copilot based on our commits from the last 7 days, what could potentially be causing this error?

PrismaClientKnownRequestError: ...

Based on my analysis of the codebase, the error is caused by Prisma's cascade delete handling with PlanetScale's relationMode.

Here are the likely sources from recent changes:

Root Cause:
The schema uses relationMode = "prisma" (required for PlanetScale), which means Prisma handles referential integrity at the application level. When deleting a Partner, Prisma attempts to cascade delete related ProgramEnrollment records, but the error suggests it's trying to reference ProgramEnrollment.programId which appears to be unavailable in the deletion context.

Potential Issues in Recent Code:

  1. apps/web/lib/api/partners/bulk-delete-partners.ts (newly added):

    • Lines 129-136: When deletePartners=true, it calls partner.deleteMany() directly, relying on Prisma's cascade deletes
    • This skips manual deletion of ProgramEnrollment records (which is only done when deletePartners=false)
    • Should manually delete ProgramEnrollments BEFORE deleting Partners
  2. apps/web/app/(ee)/api/admin/delete-partner-account/route.ts (newly added):

    • Line 109: Calls partner.delete() directly after deleting links
    • Relies on cascade to clean up ProgramEnrollments and other related records
    • Should explicitly delete all cascading relations first

Fix Required:
Manually delete ProgramEnrollment records (and other cascading relations) BEFORE calling partner.delete() or partner.deleteMany(), since PlanetScale doesn't support database-level foreign key constraints.

The schema shows 30+ models with partnerId fields that need proper cleanup sequencing.

Copilot AI changed the title [WIP] Investigate potential sources of recent error Investigation: Partner deletion cascade error with Prisma + PlanetScale Dec 26, 2025
@steven-tey steven-tey closed this Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants