[GH-ISSUE #998] How can I make the user ID an auto-incrementing integer? #25865

Closed
opened 2026-04-17 16:09:46 -05:00 by GiteaMirror · 9 comments
Owner

Originally created by @marcusleonas on GitHub (Dec 23, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/998

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a new nextjs app (create-next-app)
  2. Install drizzle-orm
  3. Install and setup better-auth using email & password, but change the user ID col in the database to be serial not string, and update other FKs to be integer.
  4. Disable the generateId function by setting it to false in the better-auth client.

Current vs. Expected behavior

I would like to get type correct, but don't know how. The userid is still showing up as a string on the better-auth functions (e.g. useSession), i need it to be number.

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg" }),
  emailAndPassword: {
    enabled: true,
  },
  plugins: [nextCookies()],
  advanced: {
    generateId: false,
  },
});

What version of Better Auth are you using?

1.1.1

Provide environment information

- OS: Mac OS Sequoia
- Browser: Arc (chromium)

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

Types

Auth config (if applicable)

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: "pg" }),
  emailAndPassword: {
    enabled: true,
  },
  plugins: [nextCookies()],
  advanced: {
    generateId: false,
  },
});

Additional context

No response

Originally created by @marcusleonas on GitHub (Dec 23, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/998 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a new nextjs app (create-next-app) 2. Install drizzle-orm 3. Install and setup better-auth using email & password, but change the user ID col in the database to be serial not string, and update other FKs to be integer. 4. Disable the generateId function by setting it to false in the better-auth client. ### Current vs. Expected behavior I would like to get type correct, but don't know how. The userid is still showing up as a string on the better-auth functions (e.g. useSession), i need it to be number. ```ts export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg" }), emailAndPassword: { enabled: true, }, plugins: [nextCookies()], advanced: { generateId: false, }, }); ``` ### What version of Better Auth are you using? 1.1.1 ### Provide environment information ```bash - OS: Mac OS Sequoia - Browser: Arc (chromium) ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg" }), emailAndPassword: { enabled: true, }, plugins: [nextCookies()], advanced: { generateId: false, }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 16:09:46 -05:00
Author
Owner

@marcusleonas commented on GitHub (Dec 23, 2024):

I also found a way to create my custom user type, still doesn't apply to the functions though:

type DefaultUser = typeof auth.$Infer.Session.user;
export interface User extends Omit<DefaultUser, "id"> {
  id: number;
}
<!-- gh-comment-id:2559477603 --> @marcusleonas commented on GitHub (Dec 23, 2024): I also found a way to create my custom user type, still doesn't apply to the functions though: ```ts type DefaultUser = typeof auth.$Infer.Session.user; export interface User extends Omit<DefaultUser, "id"> { id: number; } ```
Author
Owner

@daveycodez commented on GitHub (Dec 23, 2024):

You just change it in your Drizzle schema.ts file, here I am using uuidv7 which is a timebased uuid. But you can use serial() type here.

Do note though that using an integer type for users will break the JWT plugin if you plan on using that

export const users = pgTable("users", {
	id: uuid("id").primaryKey().notNull().default(sql`uuid_generate_v7()`),
	name: text().notNull(),
	email: text().notNull(),
	emailVerified: boolean("email_verified").notNull(),
	image: text(),
	isAnonymous: boolean("is_anonymous"),
	createdAt: timestamp("created_at").notNull(),
	updatedAt: timestamp("updated_at").notNull(),
}, (table) => [
	unique("users_email_unique").on(table.email),
])
<!-- gh-comment-id:2560120470 --> @daveycodez commented on GitHub (Dec 23, 2024): You just change it in your Drizzle schema.ts file, here I am using uuidv7 which is a timebased uuid. But you can use serial() type here. Do note though that using an integer type for users will break the JWT plugin if you plan on using that ```ts export const users = pgTable("users", { id: uuid("id").primaryKey().notNull().default(sql`uuid_generate_v7()`), name: text().notNull(), email: text().notNull(), emailVerified: boolean("email_verified").notNull(), image: text(), isAnonymous: boolean("is_anonymous"), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").notNull(), }, (table) => [ unique("users_email_unique").on(table.email), ]) ```
Author
Owner

@abegehr commented on GitHub (Jan 31, 2025):

For sqlite, the primary key is usually an int: https://www.tutorialspoint.com/sqlite/sqlite_primary_key.htm - so would be nice to have support for integer PKs, however not finding anything regarding that in the core schema: https://www.better-auth.com/docs/concepts/database#core-schema

<!-- gh-comment-id:2626324047 --> @abegehr commented on GitHub (Jan 31, 2025): For sqlite, the primary key is usually an int: https://www.tutorialspoint.com/sqlite/sqlite_primary_key.htm - so would be nice to have support for integer PKs, however not finding anything regarding that in the core schema: https://www.better-auth.com/docs/concepts/database#core-schema
Author
Owner

@abegehr commented on GitHub (Feb 6, 2025):

With schema using id: number, better-auth functions return id: string, for example on the getSession() endpoint. Ref: https://github.com/better-auth/better-auth/issues/227#issuecomment-2567698105

EDIT: It's also not possible to override id type to number on the fields arg on schemas. Therefore doesn't seem possible currently to use number ids.

<!-- gh-comment-id:2639557271 --> @abegehr commented on GitHub (Feb 6, 2025): With schema using `id: number`, better-auth functions return `id: string`, for example on the getSession() endpoint. Ref: https://github.com/better-auth/better-auth/issues/227#issuecomment-2567698105 EDIT: It's also not possible to override id type to number on the fields arg on schemas. Therefore doesn't seem possible currently to use number ids.
Author
Owner

@marcusleonas commented on GitHub (Feb 24, 2025):

With schema using id: number, better-auth functions return id: string, for example on the getSession() endpoint. Ref: #227 (comment)

EDIT: It's also not possible to override id type to number on the fields arg on schemas. Therefore doesn't seem possible currently to use number ids.

This is the issue i'm having, I really like better-auth, but not being able to change the type of the id returned by the client is a deal breaker for me

<!-- gh-comment-id:2678322047 --> @marcusleonas commented on GitHub (Feb 24, 2025): > With schema using `id: number`, better-auth functions return `id: string`, for example on the getSession() endpoint. Ref: [#227 (comment)](https://github.com/better-auth/better-auth/issues/227#issuecomment-2567698105) > > EDIT: It's also not possible to override id type to number on the fields arg on schemas. Therefore doesn't seem possible currently to use number ids. This is the issue i'm having, I really like better-auth, but not being able to change the type of the id returned by the client is a deal breaker for me
Author
Owner

@daveycodez commented on GitHub (Feb 24, 2025):

A workaround would be to create a wrapper function for getSession or any other functions and just typecast the id there (in the meantime)

<!-- gh-comment-id:2679486603 --> @daveycodez commented on GitHub (Feb 24, 2025): A workaround would be to create a wrapper function for getSession or any other functions and just typecast the id there (in the meantime)
Author
Owner

@benkingcode commented on GitHub (Mar 21, 2025):

Is this really not supported? I'm getting Zod errors from linkSocial because my userId is a number and not a string

<!-- gh-comment-id:2743479651 --> @benkingcode commented on GitHub (Mar 21, 2025): Is this really not supported? I'm getting Zod errors from `linkSocial` because my `userId` is a number and not a string
Author
Owner

@iRoachie commented on GitHub (Mar 21, 2025):

@benkingcode It is not supported. It’s been said multiple times in many issues.

Even if you change the id to be a int in your schema and disable the generate id feature, the client types will always type the ids as a string

<!-- gh-comment-id:2743896906 --> @iRoachie commented on GitHub (Mar 21, 2025): @benkingcode It is not supported. It’s been said multiple times in many issues. Even if you change the id to be a int in your schema and disable the generate id feature, the client types will always type the ids as a string
Author
Owner

@daveycodez commented on GitHub (Mar 21, 2025):

@benkingcode It is not supported. It’s been said multiple times in many issues.

Even if you change the id to be a int in your schema and disable the generate id feature, the client types will always type the ids as a string

The workaround is to manually typecast the User object, but they are planning for more customizable types in the future

<!-- gh-comment-id:2743941745 --> @daveycodez commented on GitHub (Mar 21, 2025): > [@benkingcode](https://github.com/benkingcode) It is not supported. It’s been said multiple times in many issues. > > Even if you change the id to be a int in your schema and disable the generate id feature, the client types will always type the ids as a string The workaround is to manually typecast the User object, but they are planning for more customizable types in the future
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25865