[PR #4630] [MERGED] fix(prisma): handle optional field relation types correctly #5492

Closed
opened 2026-03-13 12:24:50 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4630
Author: @LiYulin-s
Created: 9/13/2025
Status: Merged
Merged: 9/13/2025
Merged by: @himself65

Base: canaryHead: fix/optional-field-prisma


📝 Commits (1)

  • 8dbf7f0 fix(prisma): handle optional field relation types correctly

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 packages/cli/src/generators/prisma.ts (+3 -1)

📄 Description

When configuring an additionalFields on the user with required: false and a references model, the better-auth cli generate command correctly creates an optional foreign key field (e.g., extension String?). However, it incorrectly generates the corresponding relation field as non-optional (e.g., userextension UserExtension), which results in an invalid Prisma schema.

Steps to Reproduce

  1. Configure better-auth with the following user.additionalFields
// auth.ts
// ...
user: {
    additionalFields: {
        extension: {
            type: "string",
            required: false,
            unique: true,
            references: {
                model: "UserExtension",
                field: "id",
                onDelete: "cascade",
            }
        }
    }
},
// ...
  1. Run the schema generation command
npx better-auth generate
  1. Observe the generated schema.prisma file. The User model will contain
model User {
  // ...
  extension     String?
  userextension UserExtension @relation(fields: [extension], references: [id], onDelete: Cascade) // This line is incorrect
  // ...
}

This pull request introduces a small change to the way field types are generated in the Prisma schema generator. The update ensures that optional fields in referenced custom models are correctly marked with a ? in their type definition. This patch fix the generated Prisma schema for optional relations.


Summary by cubic

Fixes Prisma schema generation for optional relations. The CLI now marks relation fields as optional when the referenced user additional field has required: false, preventing invalid schemas.

  • Bug Fixes
    • Append "?" to relation field types when additionalFields[...].required is false.

🔄 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/4630 **Author:** [@LiYulin-s](https://github.com/LiYulin-s) **Created:** 9/13/2025 **Status:** ✅ Merged **Merged:** 9/13/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/optional-field-prisma` --- ### 📝 Commits (1) - [`8dbf7f0`](https://github.com/better-auth/better-auth/commit/8dbf7f051a6551a3687bddc14c92560ce74ce51d) fix(prisma): handle optional field relation types correctly ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/cli/src/generators/prisma.ts` (+3 -1) </details> ### 📄 Description When configuring an `additionalFields` on the user with `required: false` and a references model, the better-auth cli generate command correctly creates an optional foreign key field (e.g., extension String?). However, it incorrectly generates the corresponding relation field as non-optional (e.g., userextension UserExtension), which results in an invalid Prisma schema. ## Steps to Reproduce 1. Configure better-auth with the following `user.additionalFields` ```ts // auth.ts // ... user: { additionalFields: { extension: { type: "string", required: false, unique: true, references: { model: "UserExtension", field: "id", onDelete: "cascade", } } } }, // ... ``` 2. Run the schema generation command ```sh npx better-auth generate ``` 3. Observe the generated `schema.prisma` file. The User model will contain ```prisma model User { // ... extension String? userextension UserExtension @relation(fields: [extension], references: [id], onDelete: Cascade) // This line is incorrect // ... } ``` This pull request introduces a small change to the way field types are generated in the Prisma schema generator. The update ensures that optional fields in referenced custom models are correctly marked with a `?` in their type definition. This patch fix the generated Prisma schema for optional relations. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes Prisma schema generation for optional relations. The CLI now marks relation fields as optional when the referenced user additional field has required: false, preventing invalid schemas. - **Bug Fixes** - Append "?" to relation field types when additionalFields[...].required is false. <!-- 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 12:24:50 -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#5492