[GH-ISSUE #5849] defaultValue is not applying in prisma schema #27681

Closed
opened 2026-04-17 18:50:11 -05:00 by GiteaMirror · 9 comments
Owner

Originally created by @harshmangalam on GitHub (Nov 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5849

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  • Create project with better auth + nextjs + prisma + postgresql
  • Add additionalFields
 additionalFields: {
      role: {
        type: "string",
        required: false,
        defaultValue: "user",
        input: false,
      },
    },
  • Generate schema
bunx @better-auth/cli generate
  • prisma user schema have not default value in role
model User {
  id            String    @id @default(uuid())
  name          String
  email         String
  emailVerified Boolean   @default(false)
  image         String?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @default(now()) @updatedAt
  sessions      Session[]
  accounts      Account[]

  role String?

  @@unique([email])
  @@map("user")
}

Current vs. Expected behavior

Expected behaviour

model User {
  id            String    @id @default(uuid())
  name          String
  email         String
  emailVerified Boolean   @default(false)
  image         String?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @default(now()) @updatedAt
  sessions      Session[]
  accounts      Account[]

  role String? @default("user")

  @@unique([email])
  @@map("user")
}

What version of Better Auth are you using?

^1.3.34

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.5.0: Tue Apr 22 19:48:46 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8103",
    "release": "24.5.0",
    "cpuCount": 8,
    "cpuModel": "Apple M1",
    "totalMemory": "8.00 GB",
    "freeMemory": "0.10 GB"
  },
  "node": {
    "version": "v24.5.0",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.3.1"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "16.0.1"
    },
    {
      "name": "react",
      "version": "19.2.0"
    }
  ],
  "databases": [
    {
      "name": "@prisma/client",
      "version": "^6.9.0"
    }
  ],
  "betterAuth": {
    "version": "^1.3.34",
    "config": {
      "emailAndPassword": {
        "enabled": true,
        "autoSignIn": false
      },
      "user": {
        "additionalFields": {
          "role": {
            "type": "string",
            "required": false,
            "defaultValue": "user",
            "input": false
          }
        }
      },
      "plugins": [
        {
          "name": "next-cookies",
          "config": {
            "id": "next-cookies",
            "hooks": {
              "after": [
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { prisma } from "./db";
import { nextCookies } from "better-auth/next-js";

export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  emailAndPassword: {
    enabled: true,
    autoSignIn: false,
  },
  user: {
    additionalFields: {
      role: {
        type: "string",
        required: false,
        defaultValue: "user",
        input: false,
      },
    },
  },
  plugins: [nextCookies()],
});

Additional context

No response

Originally created by @harshmangalam on GitHub (Nov 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5849 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce - Create project with better auth + nextjs + prisma + postgresql - Add additionalFields ```js additionalFields: { role: { type: "string", required: false, defaultValue: "user", input: false, }, }, ``` - Generate schema ```js bunx @better-auth/cli generate ``` - prisma user schema have not default value in `role` ```js model User { id String @id @default(uuid()) name String email String emailVerified Boolean @default(false) image String? createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt sessions Session[] accounts Account[] role String? @@unique([email]) @@map("user") } ``` ### Current vs. Expected behavior Expected behaviour ```js model User { id String @id @default(uuid()) name String email String emailVerified Boolean @default(false) image String? createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt sessions Session[] accounts Account[] role String? @default("user") @@unique([email]) @@map("user") } ``` ### What version of Better Auth are you using? ^1.3.34 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.5.0: Tue Apr 22 19:48:46 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8103", "release": "24.5.0", "cpuCount": 8, "cpuModel": "Apple M1", "totalMemory": "8.00 GB", "freeMemory": "0.10 GB" }, "node": { "version": "v24.5.0", "env": "development" }, "packageManager": { "name": "bun", "version": "1.3.1" }, "frameworks": [ { "name": "next", "version": "16.0.1" }, { "name": "react", "version": "19.2.0" } ], "databases": [ { "name": "@prisma/client", "version": "^6.9.0" } ], "betterAuth": { "version": "^1.3.34", "config": { "emailAndPassword": { "enabled": true, "autoSignIn": false }, "user": { "additionalFields": { "role": { "type": "string", "required": false, "defaultValue": "user", "input": false } } }, "plugins": [ { "name": "next-cookies", "config": { "id": "next-cookies", "hooks": { "after": [ {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { prisma } from "./db"; import { nextCookies } from "better-auth/next-js"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true, autoSignIn: false, }, user: { additionalFields: { role: { type: "string", required: false, defaultValue: "user", input: false, }, }, }, plugins: [nextCookies()], }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 18:50:11 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Nov 8, 2025):

It looks like the Prisma schema isn’t picking up the default value for the additional field “role.” Make sure you’re using the supported syntax for additional fields (as in your config) and that your adapter version fully supports default values for additionalFields. As a temporary workaround you could manually add the @default("user") annotation in the generated schema, and you might consider pinning to an earlier release if that behavior worked before. Have you checked that your adapter (and CLI generate command) is up to date, and do you see this issue on a minimal reproduction? Tag @better-auth-agent if you would like an answer from the Agent. What additional troubleshooting steps have you already tried?

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3506107365 --> @better-auth-agent[bot] commented on GitHub (Nov 8, 2025): It looks like the Prisma schema isn’t picking up the default value for the additional field “role.” Make sure you’re using the supported syntax for additional fields (as in your config) and that your adapter version fully supports default values for additionalFields. As a temporary workaround you could manually add the @default("user") annotation in the generated schema, and you might consider pinning to an earlier release if that behavior worked before. Have you checked that your adapter (and CLI generate command) is up to date, and do you see this issue on a minimal reproduction? Tag @better-auth-agent if you would like an answer from the Agent. What additional troubleshooting steps have you already tried? <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@dosubot[bot] commented on GitHub (Nov 8, 2025):

This is a known limitation in Better Auth's Prisma schema generation: string-type defaultValue in additionalFields (like role: { defaultValue: "user" }) is not mapped to Prisma's @default("user") attribute in the generated schema. The CLI only applies defaults for booleans and some special cases, but skips strings entirely, so your config won't affect the Prisma schema as expected see source.

Workarounds: you can manually edit your Prisma schema after generation to add @default("user"), or use a database hook to set the value during user creation. Some users have tried wrapping the default in extra quotes (e.g., defaultValue: "'user'"), but this is not ideal and may not work reliably see related issue.

This is suited for GitHub as a bug/feature request, since the expected behavior is for string defaults to be reflected in the schema. If you want this fixed automatically, you can track or contribute to related issues and PRs.

Let me know if you want more details on manual workarounds or how to set up a database hook.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3506109412 --> @dosubot[bot] commented on GitHub (Nov 8, 2025): <!-- Answer --> This is a known limitation in Better Auth's Prisma schema generation: string-type `defaultValue` in `additionalFields` (like `role: { defaultValue: "user" }`) is not mapped to Prisma's `@default("user")` attribute in the generated schema. The CLI only applies defaults for booleans and some special cases, but skips strings entirely, so your config won't affect the Prisma schema as expected [see source](https://github.com/better-auth/better-auth/blob/ea818f266ca3d3d70bbba44af57549030fa278d3/packages/cli/src/generators/prisma.ts#L9-L264). Workarounds: you can manually edit your Prisma schema after generation to add `@default("user")`, or use a database hook to set the value during user creation. Some users have tried wrapping the default in extra quotes (e.g., `defaultValue: "'user'"`), but this is not ideal and may not work reliably [see related issue](https://github.com/better-auth/better-auth/issues/2786). This is suited for GitHub as a bug/feature request, since the expected behavior is for string defaults to be reflected in the schema. If you want this fixed automatically, you can track or contribute to related issues and PRs. Let me know if you want more details on manual workarounds or how to set up a database hook. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/96e78f39-dcf0-4ba0-93fa-3cf08e26de3a?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/5849)
Author
Owner

@rovertrack commented on GitHub (Nov 8, 2025):

model User {
  id               String    @id
  name             String
  email            String
  emailVerified    Boolean   @default(false)
  image            String?
  createdAt        DateTime  @default(now())
  updatedAt        DateTime  @updatedAt
  numberofaccounts Int[]     @default([1,2,3])
  role             String[]  @default(["user","admin","guest"])
  sessions         Session[]
  accounts         Account[]

  @@unique([email])
  @@map("user")
`}```

does this solve the issue ?
was generated after solving the issue locally.

<!-- gh-comment-id:3506619731 --> @rovertrack commented on GitHub (Nov 8, 2025): ``` model User { id String @id name String email String emailVerified Boolean @default(false) image String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt numberofaccounts Int[] @default([1,2,3]) role String[] @default(["user","admin","guest"]) sessions Session[] accounts Account[] @@unique([email]) @@map("user") `}``` ``` does this solve the issue ? was generated after solving the issue locally.
Author
Owner

@harshmangalam commented on GitHub (Nov 8, 2025):

model User {
  id               String    @id
  name             String
  email            String
  emailVerified    Boolean   @default(false)
  image            String?
  createdAt        DateTime  @default(now())
  updatedAt        DateTime  @updatedAt
  numberofaccounts Int[]     @default([1,2,3])
  role             String[]  @default(["user","admin","guest"])
  sessions         Session[]
  accounts         Account[]

  @@unique([email])
  @@map("user")
`}```

does this solve the issue ? was generated after solving the issue locally.

can you try to generate @default("user") instead of @default(["user","admin","guest"])

<!-- gh-comment-id:3506760249 --> @harshmangalam commented on GitHub (Nov 8, 2025): > ``` > model User { > id String @id > name String > email String > emailVerified Boolean @default(false) > image String? > createdAt DateTime @default(now()) > updatedAt DateTime @updatedAt > numberofaccounts Int[] @default([1,2,3]) > role String[] @default(["user","admin","guest"]) > sessions Session[] > accounts Account[] > > @@unique([email]) > @@map("user") > `}``` > ``` > > does this solve the issue ? was generated after solving the issue locally. can you try to generate @default("user") instead of @default(["user","admin","guest"])
Author
Owner

@rovertrack commented on GitHub (Nov 8, 2025):

sure

model User {
  id            String    @id
  name          String
  email         String
  emailVerified Boolean   @default(false)
  image         String?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  isActive      Boolean   @default(true)
  profileData   Json?     @default("{\"age\":30,\"gender\":\"female\",\"location\":\"New York\"}")
  tags          String[]  @default(["general","premium"])
  roles         String    @default("admin")
  preferences   Json?     @default("{\"notifications\":true,\"newsletter\":false}")
  age           Int       @default(25)
  points        Int[]     @default([100,200,300])
  sessions      Session[]
  accounts      Account[]

  @@unique([email])
  @@map("user")
}

this was generated after doing changes locally will raise PR after testing

<!-- gh-comment-id:3506796669 --> @rovertrack commented on GitHub (Nov 8, 2025): sure ``` model User { id String @id name String email String emailVerified Boolean @default(false) image String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt isActive Boolean @default(true) profileData Json? @default("{\"age\":30,\"gender\":\"female\",\"location\":\"New York\"}") tags String[] @default(["general","premium"]) roles String @default("admin") preferences Json? @default("{\"notifications\":true,\"newsletter\":false}") age Int @default(25) points Int[] @default([100,200,300]) sessions Session[] accounts Account[] @@unique([email]) @@map("user") } ``` this was generated after doing changes locally will raise PR after testing
Author
Owner

@rovertrack commented on GitHub (Nov 11, 2025):

@harshmangalam checkout updated version it's working for postgresql.

<!-- gh-comment-id:3518382081 --> @rovertrack commented on GitHub (Nov 11, 2025): @harshmangalam checkout updated version it's working for postgresql.
Author
Owner

@harshmangalam commented on GitHub (Nov 11, 2025):

Thanks @rovertrack

<!-- gh-comment-id:3518406052 --> @harshmangalam commented on GitHub (Nov 11, 2025): Thanks @rovertrack
Author
Owner

@himself65 commented on GitHub (Nov 13, 2025):

/cc @ping-maxwell

<!-- gh-comment-id:3529931111 --> @himself65 commented on GitHub (Nov 13, 2025): /cc @ping-maxwell
Author
Owner

@dosubot[bot] commented on GitHub (Feb 12, 2026):

Hi, @harshmangalam. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary

  • You reported that the defaultValue for the "role" field in additionalFields was not reflected as @default("user") in the generated Prisma schema.
  • It was clarified this is a known limitation where string defaults are not applied automatically in Prisma schema generation.
  • A contributor, rovertrack, shared a local fix that correctly generates the @default values.
  • An updated version incorporating this fix was released and confirmed to work properly with PostgreSQL.
  • You acknowledged that the updated version resolved the issue.

Next Steps

  • Please confirm if this issue is still relevant with the latest version of better-auth by commenting here.
  • Otherwise, I will automatically close this issue in 7 days.

Thanks for your understanding and contribution!

<!-- gh-comment-id:3891857366 --> @dosubot[bot] commented on GitHub (Feb 12, 2026): Hi, @harshmangalam. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary** - You reported that the defaultValue for the "role" field in additionalFields was not reflected as @default("user") in the generated Prisma schema. - It was clarified this is a known limitation where string defaults are not applied automatically in Prisma schema generation. - A contributor, rovertrack, shared a local fix that correctly generates the @default values. - An updated version incorporating this fix was released and confirmed to work properly with PostgreSQL. - You acknowledged that the updated version resolved the issue. **Next Steps** - Please confirm if this issue is still relevant with the latest version of better-auth by commenting here. - Otherwise, I will automatically close this issue in 7 days. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27681