CLI generate breaking on incorrect/unexpected additionalField types #1692

Closed
opened 2026-03-13 08:56:48 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @thyngster on GitHub (Aug 13, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

My problem was that I was initialy testing MongoDB Adapter and I did the following

     is_subscribed: {
        type: "object",
        required: false,
        defaultValue: () => ({ newsletter: false })
      },

While this wasn't an issue on Mongo when switching to PG it was giving a non-clear error, I think it would be a good idea adding some proper error handling on this part,

file:///C:/Users/David/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2175
      return typeMap[type][databaseType];
                          ^

TypeError: Cannot read properties of undefined (reading 'pg')
    at getType (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2175:27)
    at file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2203:18
    at Array.map (<anonymous>)
    at generateDrizzleSchema (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2201:28)
    at generateSchema (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2459:12)
    at Command.generateAction (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2502:24)

Current vs. Expected behavior

Current Code

    const type = field.type;
     const typeMap = {
       string: {
         sqlite: `text('${name}')`,
         pg: `text('${name}')`,
         mysql: field.unique ? `varchar('${name}', { length: 255 })` : field.references ? `varchar('${name}', { length: 36 })` : `text('${name}')`
       },
       boolean: {
         sqlite: `integer('${name}', { mode: 'boolean' })`,
         pg: `boolean('${name}')`,
         mysql: `boolean('${name}')`
       },
       number: {
         sqlite: `integer('${name}')`,
         pg: field.bigint ? `bigint('${name}', { mode: 'number' })` : `integer('${name}')`,
         mysql: field.bigint ? `bigint('${name}', { mode: 'number' })` : `int('${name}')`
       },
       date: {
         sqlite: `integer('${name}', { mode: 'timestamp' })`,
         pg: `timestamp('${name}')`,
         mysql: `timestamp('${name}')`
       },
       "number[]": {
         sqlite: `integer('${name}').array()`,
         pg: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `integer('${name}').array()`,
         mysql: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `int('${name}').array()`
       },
       "string[]": {
         sqlite: `text('${name}').array()`,
         pg: `text('${name}').array()`,
         mysql: `text('${name}').array()`
       }
     };

     return typeMap[type][databaseType];

So adding an extra check to validate thefield type would prevent this:

 if (!typeMap[type]) {
  throw new Error(`'${type}' is not allowed in the additional field definition`);
 }

What version of Better Auth are you using?

1.3.5

System info

all

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

Types, Other

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @thyngster on GitHub (Aug 13, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce My problem was that I was initialy testing MongoDB Adapter and I did the following ``` is_subscribed: { type: "object", required: false, defaultValue: () => ({ newsletter: false }) }, ``` While this wasn't an issue on Mongo when switching to PG it was giving a non-clear error, I think it would be a good idea adding some proper error handling on this part, ``` file:///C:/Users/David/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2175 return typeMap[type][databaseType]; ^ TypeError: Cannot read properties of undefined (reading 'pg') at getType (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2175:27) at file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2203:18 at Array.map (<anonymous>) at generateDrizzleSchema (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2201:28) at generateSchema (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2459:12) at Command.generateAction (file:///C:/Users/xxx/AppData/Local/npm-cache/_npx/167ca1f116d365e6/node_modules/@better-auth/cli/dist/index.mjs:2502:24) ``` ### Current vs. Expected behavior Current Code ``` const type = field.type; const typeMap = { string: { sqlite: `text('${name}')`, pg: `text('${name}')`, mysql: field.unique ? `varchar('${name}', { length: 255 })` : field.references ? `varchar('${name}', { length: 36 })` : `text('${name}')` }, boolean: { sqlite: `integer('${name}', { mode: 'boolean' })`, pg: `boolean('${name}')`, mysql: `boolean('${name}')` }, number: { sqlite: `integer('${name}')`, pg: field.bigint ? `bigint('${name}', { mode: 'number' })` : `integer('${name}')`, mysql: field.bigint ? `bigint('${name}', { mode: 'number' })` : `int('${name}')` }, date: { sqlite: `integer('${name}', { mode: 'timestamp' })`, pg: `timestamp('${name}')`, mysql: `timestamp('${name}')` }, "number[]": { sqlite: `integer('${name}').array()`, pg: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `integer('${name}').array()`, mysql: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `int('${name}').array()` }, "string[]": { sqlite: `text('${name}').array()`, pg: `text('${name}').array()`, mysql: `text('${name}').array()` } }; return typeMap[type][databaseType]; ``` So adding an extra check to validate thefield type would prevent this: ``` if (!typeMap[type]) { throw new Error(`'${type}' is not allowed in the additional field definition`); } ``` ### What version of Better Auth are you using? 1.3.5 ### System info ```bash all ``` ### Which area(s) are affected? (Select all that apply) Types, Other ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 08:56:48 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 13, 2025):

Hi @thyngster! I'm Dosu and I’m helping the better-auth team.

You're right—there's currently no validation for unsupported additionalField types in the CLI generate command, which leads to the TypeError you saw when using a type like "object" with SQL adapters such as Postgres. The code just tries to access the type mapping and fails if the type isn't present, resulting in a confusing error message rather than a clear explanation of what's wrong. No documentation or recent changes address this yet, and your suggestion to add a check like:

if (!typeMap[type]) {
  throw new Error(`'${type}' is not allowed in the additional field definition`);
}

would definitely improve the developer experience by surfacing a clear, actionable error early in the process see code reference.

For SQL adapters, stick to the supported primitive types in the typeMap (string, boolean, number, date, number[], string[]). If you need to store complex data, consider serializing your object to a string (e.g., JSON) and using a string field, or use a JSON column if your database supports it.

This is a valid area for improvement in the CLI, and your proposed fix is spot-on.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Aug 13, 2025): <!-- Greeting --> Hi @thyngster! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You're right—there's currently no validation for unsupported additionalField types in the CLI generate command, which leads to the TypeError you saw when using a type like `"object"` with SQL adapters such as Postgres. The code just tries to access the type mapping and fails if the type isn't present, resulting in a confusing error message rather than a clear explanation of what's wrong. No documentation or recent changes address this yet, and your suggestion to add a check like: ```js if (!typeMap[type]) { throw new Error(`'${type}' is not allowed in the additional field definition`); } ``` would definitely improve the developer experience by surfacing a clear, actionable error early in the process [see code reference](https://github.com/better-auth/better-auth/blob/c7a272f26137bb1eeafae267d460fc105f4ad714/packages/cli/src/commands/generate.ts). For SQL adapters, stick to the supported primitive types in the typeMap (`string`, `boolean`, `number`, `date`, `number[]`, `string[]`). If you need to store complex data, consider serializing your object to a string (e.g., JSON) and using a `string` field, or use a JSON column if your database supports it. This is a valid area for improvement in the CLI, and your proposed fix is spot-on. <!-- 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/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8e3e9982-112d-423b-a4f7-20786d8433de?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/3965)
Author
Owner

@xuzuodong commented on GitHub (Aug 19, 2025):

Is there any temporarily workaround for this? 👀

@xuzuodong commented on GitHub (Aug 19, 2025): Is there any temporarily workaround for this? 👀
Author
Owner

@thyngster commented on GitHub (Aug 19, 2025):

Stick to using string, boolean, number, date, number[], string[] primitives if using Postgres, SQLIte or MySQL

@thyngster commented on GitHub (Aug 19, 2025): Stick to using string, boolean, number, date, number[], string[] primitives if using Postgres, SQLIte or MySQL
Author
Owner

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

Hi, @thyngster. 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 CLI generate command fails with a TypeError when unsupported additionalField types like "object" are used in SQL databases.
  • I confirmed that the lack of validation causes this unclear error.
  • The recommended workaround is to use only supported primitive types or serialize complex objects as strings.
  • You reiterated this workaround applies to SQL databases such as Postgres, SQLite, and MySQL.
  • Explicit error handling for unsupported types is recognized as a needed improvement but not yet implemented.

Next Steps

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Nov 18, 2025): Hi, @thyngster. 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 CLI generate command fails with a TypeError when unsupported additionalField types like "object" are used in SQL databases. - I confirmed that the lack of validation causes this unclear error. - The recommended workaround is to use only supported primitive types or serialize complex objects as strings. - You reiterated this workaround applies to SQL databases such as Postgres, SQLite, and MySQL. - Explicit error handling for unsupported types is recognized as a needed improvement but not yet implemented. **Next Steps** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - Otherwise, this issue will be automatically closed in 7 days. Thank you 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#1692