[PR #6271] feat(prisma): add option to rename relations in schema and adapter #6556

Open
opened 2026-03-13 13:03:12 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6271
Author: @GautamBytes
Created: 11/24/2025
Status: 🔄 Open

Base: canaryHead: feat/prisma-custom-relations


📝 Commits (6)

  • e51216f add support for custom relation names
  • 2325a0a test(cli): add test case for custom prisma relations
  • 76ed25e Merge branch 'canary' into feat/prisma-custom-relations
  • c3ff56b Merge branch 'canary' into feat/prisma-custom-relations
  • e24834d add documentation to prisma.mdx
  • 62c6da0 Merge branch 'canary' into feat/prisma-custom-relations

📊 Changes

6 files changed (+196 additions, -19 deletions)

View changed files

📝 docs/content/docs/adapters/prisma.mdx (+24 -0)
📝 packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts (+26 -17)
📝 packages/cli/src/generators/prisma.ts (+25 -2)
packages/cli/test/__snapshots__/schema-relations.prisma (+72 -0)
📝 packages/cli/test/generate.test.ts (+37 -0)
📝 packages/core/src/types/init-options.ts (+12 -0)

📄 Description

This PR addresses #6262 by allowing users to customize relation names in their Prisma schema configuration. Previously, relation names were hardcoded (e.g., sessions or user), which caused conflicts with existing databases or strict naming conventions (e.g., requiring PascalCase UserSessions).

Changes

  • packages/core: Added optional relations?: Record<string, string> configuration to user, session, and account options.
  • packages/cli: Updated generators/prisma.ts to respect custom relation names for both:
  • Forward Relations: (e.g., user field on the Session model).
  • Back Relations: (e.g., sessions field on the User model).
  • packages/better-auth: Updated prisma-adapter.ts runtime logic to dynamically look up the correct relation key during joins.
  • Added safety checks to handle singular/plural mismatch (e.g., if config maps "session" but internal logic looks for "sessions").

Summary by cubic

Adds support for custom Prisma relation field names in the CLI generator and adapter so projects can match existing DB conventions. Aligns with Linear #6262 by removing hardcoded names and handling singular/plural safely.

  • New Features

    • core: Added relations?: Record<string, string> to user, session, and account options (key = default name, value = custom name).
    • cli: Prisma schema generator applies custom relation names for forward and back relations, with singular/plural based on FK uniqueness and usePlural.
    • adapter: Runtime join logic resolves the correct relation key from config and falls back cleanly, including singular/plural mismatch handling.
  • Migration

    • To rename a relation, set options..relations = { "": "" } (e.g., { "sessions": "UserSessions" }). No change needed if defaults are fine.

Written for commit 62c6da0d54. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/6271 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 11/24/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/prisma-custom-relations` --- ### 📝 Commits (6) - [`e51216f`](https://github.com/better-auth/better-auth/commit/e51216f239e774c39bb8960718757933820c4cce) add support for custom relation names - [`2325a0a`](https://github.com/better-auth/better-auth/commit/2325a0a7404b82901c01df863b6c4cbc3b777159) test(cli): add test case for custom prisma relations - [`76ed25e`](https://github.com/better-auth/better-auth/commit/76ed25ed21f4c69ca873a768db31b1a91e4d478d) Merge branch 'canary' into feat/prisma-custom-relations - [`c3ff56b`](https://github.com/better-auth/better-auth/commit/c3ff56b8ef4e2b435096a44079210c980cdffaae) Merge branch 'canary' into feat/prisma-custom-relations - [`e24834d`](https://github.com/better-auth/better-auth/commit/e24834d8d1c01f0c9bbc187d1d44f19dfc5a2035) add documentation to prisma.mdx - [`62c6da0`](https://github.com/better-auth/better-auth/commit/62c6da0d54d302e26043ff8618dfd4f8cb0d853c) Merge branch 'canary' into feat/prisma-custom-relations ### 📊 Changes **6 files changed** (+196 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/adapters/prisma.mdx` (+24 -0) 📝 `packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts` (+26 -17) 📝 `packages/cli/src/generators/prisma.ts` (+25 -2) ➕ `packages/cli/test/__snapshots__/schema-relations.prisma` (+72 -0) 📝 `packages/cli/test/generate.test.ts` (+37 -0) 📝 `packages/core/src/types/init-options.ts` (+12 -0) </details> ### 📄 Description This PR addresses #6262 by allowing users to customize relation names in their Prisma schema configuration. Previously, relation names were hardcoded (e.g., sessions or user), which caused conflicts with existing databases or strict naming conventions (e.g., requiring PascalCase UserSessions). Changes - packages/core: Added optional relations?: Record<string, string> configuration to user, session, and account options. - packages/cli: Updated generators/prisma.ts to respect custom relation names for both: - Forward Relations: (e.g., user field on the Session model). - Back Relations: (e.g., sessions field on the User model). - packages/better-auth: Updated prisma-adapter.ts runtime logic to dynamically look up the correct relation key during joins. - Added safety checks to handle singular/plural mismatch (e.g., if config maps "session" but internal logic looks for "sessions"). <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds support for custom Prisma relation field names in the CLI generator and adapter so projects can match existing DB conventions. Aligns with Linear #6262 by removing hardcoded names and handling singular/plural safely. - **New Features** - core: Added relations?: Record<string, string> to user, session, and account options (key = default name, value = custom name). - cli: Prisma schema generator applies custom relation names for forward and back relations, with singular/plural based on FK uniqueness and usePlural. - adapter: Runtime join logic resolves the correct relation key from config and falls back cleanly, including singular/plural mismatch handling. - **Migration** - To rename a relation, set options.<model>.relations = { "<default>": "<custom>" } (e.g., { "sessions": "UserSessions" }). No change needed if defaults are fine. <sup>Written for commit 62c6da0d54d302e26043ff8618dfd4f8cb0d853c. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:03:12 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6556