Skip to content

Fix synthetic back-reference generation for list-only relationships#356

Merged
borisno2 merged 1 commit intomainfrom
claude/fix-relationship-regression-H2Upx
Mar 5, 2026
Merged

Fix synthetic back-reference generation for list-only relationships#356
borisno2 merged 1 commit intomainfrom
claude/fix-relationship-regression-H2Upx

Conversation

@borisno2
Copy link
Member

@borisno2 borisno2 commented Mar 5, 2026

Fixes #355

Summary

This PR fixes a regression in version 0.19.0 where list-only relationships (both many-to-many and many-to-one) stopped generating required synthetic back-reference fields on the target model. This caused Prisma schema validation errors because Prisma requires both sides of any relationship to be explicitly defined.

Key Changes

  • Updated relationship field generation logic in prisma.ts to always generate synthetic back-reference fields for list-only relationships, regardless of whether they are many-to-many or many-to-one
  • Preserved explicit relation names when specified via relField.db?.relationName, falling back to auto-generated names otherwise
  • Updated test expectations to reflect the correct behavior:
    • Added regression test for list-only many-to-many relationships
    • Fixed test assertions that incorrectly expected synthetic fields to NOT be generated
    • Updated test descriptions to accurately reflect the required behavior

Implementation Details

The fix removes the condition that excluded many-to-many relationships from synthetic field generation. The key insight is that Prisma's implicit many-to-many relationships still require both sides to be explicitly defined in the schema, even though Prisma handles the join table implicitly. The synthetic field generation now applies consistently to all list-only references, using either the explicit relation name (if provided) or an auto-generated one based on the source model and field name.

https://claude.ai/code/session_01TQUjki3U3XpDWmvU3AbYNr

…ynthetic back-reference fields

In 0.19.0, a change to the synthetic field generation condition (from
`joinTableNaming !== 'keystone'` to `!m2mCheck.isManyToMany`) fixed
one-sided many-to-one relationships with Keystone naming but accidentally
broke list-only many-to-many relationships.

Prisma requires both sides of any implicit many-to-many relationship to be
defined. Without the synthetic back-reference field on the target model,
Prisma throws a validation error:

  Error validating field `readBy` in model `TextMessage`: The relation field
  `readBy` on model `TextMessage` is missing an opposite relation field on the
  model `User`.

Fix: always generate synthetic back-reference fields for list-only refs
(no `targetField`), regardless of whether the relationship is many-to-many
or many-to-one. The relation name now correctly uses `db.relationName` when
explicitly set, falling back to the auto-generated `${listName}_${fieldName}`.

Updates two incorrect tests that asserted synthetic fields were NOT generated
for list-only many-to-many (both with Keystone naming and explicit relationName),
and adds a dedicated regression test matching the reported bug scenario.

https://claude.ai/code/session_01TQUjki3U3XpDWmvU3AbYNr
@vercel
Copy link

vercel bot commented Mar 5, 2026

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

Project Deployment Actions Updated (UTC)
stack-docs Ready Ready Preview, Comment Mar 5, 2026 10:02am

@changeset-bot
Copy link

changeset-bot bot commented Mar 5, 2026

🦋 Changeset detected

Latest commit: 059acd1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@opensaas/stack-cli Patch
@opensaas/stack-auth Patch
@opensaas/stack-core Patch
@opensaas/stack-rag Patch
@opensaas/stack-storage-s3 Patch
@opensaas/stack-storage-vercel Patch
@opensaas/stack-storage Patch
@opensaas/stack-tiptap Patch
@opensaas/stack-ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for Core Package Coverage (./packages/core)

Status Category Percentage Covered / Total
🔵 Lines 86.17% 455 / 528
🔵 Statements 85.45% 464 / 543
🔵 Functions 97.22% 70 / 72
🔵 Branches 73.86% 325 / 440
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for UI Package Coverage (./packages/ui)

Status Category Percentage Covered / Total
🔵 Lines 76.03% 92 / 121
🔵 Statements 75.39% 95 / 126
🔵 Functions 75.6% 31 / 41
🔵 Branches 65.78% 75 / 114
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for CLI Package Coverage (./packages/cli)

Status Category Percentage Covered / Total
🔵 Lines 75.73% 1167 / 1541
🔵 Statements 75.35% 1211 / 1607
🔵 Functions 79.59% 156 / 196
🔵 Branches 65.06% 540 / 830
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/cli/src/generator/prisma.ts 89.43% 83.23% 100% 89.75% 16, 26, 40-44, 55, 68, 75, 79, 100, 106, 112-114, 117, 142, 161-163, 178-184, 214, 229, 241, 457, 469, 627
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for Auth Package Coverage (./packages/auth)

Status Category Percentage Covered / Total
🔵 Lines 64.49% 89 / 138
🔵 Statements 61.03% 94 / 154
🔵 Functions 74.46% 35 / 47
🔵 Branches 62.79% 54 / 86
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for Storage Package Coverage (./packages/storage)

Status Category Percentage Covered / Total
🔵 Lines 42.44% 73 / 172
🔵 Statements 42.77% 74 / 173
🔵 Functions 42.85% 15 / 35
🔵 Branches 40.13% 61 / 152
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for RAG Package Coverage (./packages/rag)

Status Category Percentage Covered / Total
🔵 Lines 47.97% 355 / 740
🔵 Statements 48.14% 377 / 783
🔵 Functions 54.26% 70 / 129
🔵 Branches 42.55% 180 / 423
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for Storage S3 Package Coverage (./packages/storage-s3)

Status Category Percentage Covered / Total
🔵 Lines 100% 40 / 40
🔵 Statements 100% 40 / 40
🔵 Functions 100% 9 / 9
🔵 Branches 100% 19 / 19
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Coverage Report for Storage Vercel Package Coverage (./packages/storage-vercel)

Status Category Percentage Covered / Total
🔵 Lines 100% 38 / 38
🔵 Statements 100% 38 / 38
🔵 Functions 100% 8 / 8
🔵 Branches 100% 22 / 22
File CoverageNo changed files found.
Generated in workflow #973 for commit 059acd1 by the Vitest Coverage Report Action

@borisno2 borisno2 merged commit 6d771d1 into main Mar 5, 2026
6 checks passed
@borisno2 borisno2 deleted the claude/fix-relationship-regression-H2Upx branch March 5, 2026 10:11
@github-actions github-actions bot mentioned this pull request Mar 5, 2026
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.

Regression in 0.19.0: many-to-many relationships without explicit back-ref no longer generate the back-reference field on the related model

2 participants