[GH-ISSUE #2504] AdditionalFields, defaultValue/require should apply to the db #9231

Closed
opened 2026-04-13 04:38:55 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @MarkForLoop on GitHub (May 1, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2504

Is this suited for github?

  • Yes, this is suited for github

Hi

I'm working with a Drizzle schema and encountered an issue where the defaultValue defined in the schema config isn't being correctly translated to the generated SQL schema.

I defined a hasBan field in my schema as follows:

hasBan: {
  type: "boolean",
  defaultValue: false,
  required: false,
  input: false,
},

This should ideally result in the following Drizzle schema output:

hasBan: boolean("has_ban").default(false),

However, in the generated output, the .default(false) is missing, even though I specified a defaultValue.

Previously, I had required: true in this config:

hasBan: {
  type: "boolean",
  defaultValue: false,
  required: true,
  input: false,
}

But this caused issues during user signup because required: true seems to enforce a NOT NULL constraint in the DB without falling back to the defaultValue, causing insert failures when no value was provided.

Describe the solution you'd like

if a user selects a defaultValue it should be applied to the db schema

hasBan: boolean("has_ban").default(false),

and if possible to allow

hasBan: {
  type: "boolean",
  defaultValue: false,
  required: true,
  input: false,
}

to work if no value was presented inside the create endpoint

This as a possibility of fixing the types as the database schema is saying the hasBan is boolean | null

Describe alternatives you've considered

Right as this time I am expecting the package to apply it but I feel it should be more on the database instead of the package.
I am also using the before hook to verify that it gets applied.

Additional context

The types of user is coming from the db schema

Edit:

Thinking about it more, I believe the issue stems from the fact that if you want the DB to use a defaultValue and also set required: true (so the column is NOT NULL), the signup endpoint doesn't handle it correctly—likely because it's not providing a value, assuming the DB will apply the default, but the combination of NOT NULL and no value causes a failure.

Originally created by @MarkForLoop on GitHub (May 1, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2504 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. Hi I'm working with a Drizzle schema and encountered an issue where the defaultValue defined in the schema config isn't being correctly translated to the generated SQL schema. I defined a hasBan field in my schema as follows: ```js hasBan: { type: "boolean", defaultValue: false, required: false, input: false, }, ``` This should ideally result in the following Drizzle schema output: ```ts hasBan: boolean("has_ban").default(false), ``` However, in the generated output, the .default(false) is missing, even though I specified a defaultValue. Previously, I had required: true in this config: ```js hasBan: { type: "boolean", defaultValue: false, required: true, input: false, } ``` But this caused issues during user signup because required: true seems to enforce a NOT NULL constraint in the DB without falling back to the defaultValue, causing insert failures when no value was provided. ### Describe the solution you'd like if a user selects a defaultValue it should be applied to the db schema ``` hasBan: boolean("has_ban").default(false), ``` and if possible to allow ```js hasBan: { type: "boolean", defaultValue: false, required: true, input: false, } ``` to work if no value was presented inside the create endpoint This as a possibility of fixing the types as the database schema is saying the `hasBan` is `boolean | null` ### Describe alternatives you've considered Right as this time I am expecting the package to apply it but I feel it should be more on the database instead of the package. I am also using the before hook to verify that it gets applied. ### Additional context The types of user is coming from the db schema Edit: > Thinking about it more, I believe the issue stems from the fact that if you want the DB to use a defaultValue and also set required: true (so the column is NOT NULL), the signup endpoint doesn't handle it correctly—likely because it's not providing a value, assuming the DB will apply the default, but the combination of NOT NULL and no value causes a failure.
GiteaMirror added the locked label 2026-04-13 04:38:55 -05:00
Author
Owner

@Kinfe123 commented on GitHub (May 1, 2025):

can you try installing the patch release here and let me know if the issue stills persists - npm i https://pkg.pr.new/better-auth/better-auth@2506

<!-- gh-comment-id:2844984448 --> @Kinfe123 commented on GitHub (May 1, 2025): can you try installing the patch release here and let me know if the issue stills persists - `npm i https://pkg.pr.new/better-auth/better-auth@2506`
Author
Owner

@MarkForLoop commented on GitHub (May 1, 2025):

I install the package "https://pkg.pr.new/better-auth/better-auth@2506"

I know this would be the wrong command:

pnpx @better-auth/cli@latest generate

I ran

pnpm view @better-auth/cli versions

and its coming up with a list of different versions I'm not sure which one is attached to the PR

just running it with the pnpx @better-auth/cli@latest generate still shows the same

  hasBan: boolean("has_ban"),
<!-- gh-comment-id:2845152155 --> @MarkForLoop commented on GitHub (May 1, 2025): I install the package "https://pkg.pr.new/better-auth/better-auth@2506" I know this would be the wrong command: ```bash pnpx @better-auth/cli@latest generate ``` I ran ```bash pnpm view @better-auth/cli versions ``` and its coming up with a list of different versions I'm not sure which one is attached to the PR just running it with the `pnpx @better-auth/cli@latest generate` still shows the same ```js hasBan: boolean("has_ban"), ```
Author
Owner

@Kinfe123 commented on GitHub (May 1, 2025):

since it is cli fix. you need to add a cli suffix like this - npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@2505

<!-- gh-comment-id:2845179925 --> @Kinfe123 commented on GitHub (May 1, 2025): since it is cli fix. you need to add a cli suffix like this - `npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@2505`
Author
Owner

@MarkForLoop commented on GitHub (May 1, 2025):

I see, thanks for the change. Loving the package ❤

<!-- gh-comment-id:2845563475 --> @MarkForLoop commented on GitHub (May 1, 2025): I see, thanks for the change. Loving the package ❤
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9231