using advanced.database.useNumberId generate incorrect schema. #1479

Closed
opened 2026-03-13 08:42:27 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @body20002 on GitHub (Jul 8, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

export const auth = betterAuth({
	database: drizzleAdapter(db, {
		provider: "pg",
	}),
	emailAndPassword: {
		enabled: true,
	},
	socialProviders: {
		google: {
			clientId: env.GOOGLE_CLIENT_ID,
			clientSecret: env.GOOGLE_CLIENT_SECRET,
		},
		github: {
			clientId: env.GITHUB_CLIENT_ID,
			clientSecret: env.GITHUB_CLIENT_SECRET,
		},
	},
	session: {
		expiresIn: 60 * 60 * 24 * 7, // 7 days
	},
	baseURL:
		process.env.BETTER_AUTH_URL ||
		process.env.NEXT_PUBLIC_APP_URL ||
		"http://localhost:3000",
	plugins: [
		nextCookies(),
		admin(),
		organization({
			async allowUserToCreateOrganization(_user) {
				return true;
			},
			async sendInvitationEmail(data) {
				console.log("[INVITAION]", data);
			},
			teams: {
				enabled: true,
				allowRemovingAllTeams: true,
			},
			ac,
			roles: {
				owner,
				manager,
				teamLeader,
				teamMember,
			},
		}),
		organizationManagerPlugin(), // custom plugin to add fields to database. removing this doesn't solve the issue.
	],
	advanced: {
		database: {
			useNumberId: true,
		},
	},
});

npx @better-auth/cli@latest generate --output src/server/db/schemas/public/auth.ts -y

Current vs. Expected behavior

instead of

id: int("id").autoincrement.primaryKey(),
userId: serial('user_id').primaryKey().notNull().references(()=> user.id, { onDelete: 'cascade' }),

I expect

id: serial('id').primaryKey(),
userId: integer('user_id').notNull().references(()=> user.id, { onDelete: 'cascade' }),

What version of Better Auth are you using?

1.2.12

Provide environment information

- OS: Nixos

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

Backend

Auth config (if applicable)


Additional context

No response

Originally created by @body20002 on GitHub (Jul 8, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ```ts export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", }), emailAndPassword: { enabled: true, }, socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID, clientSecret: env.GOOGLE_CLIENT_SECRET, }, github: { clientId: env.GITHUB_CLIENT_ID, clientSecret: env.GITHUB_CLIENT_SECRET, }, }, session: { expiresIn: 60 * 60 * 24 * 7, // 7 days }, baseURL: process.env.BETTER_AUTH_URL || process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000", plugins: [ nextCookies(), admin(), organization({ async allowUserToCreateOrganization(_user) { return true; }, async sendInvitationEmail(data) { console.log("[INVITAION]", data); }, teams: { enabled: true, allowRemovingAllTeams: true, }, ac, roles: { owner, manager, teamLeader, teamMember, }, }), organizationManagerPlugin(), // custom plugin to add fields to database. removing this doesn't solve the issue. ], advanced: { database: { useNumberId: true, }, }, }); ``` `npx @better-auth/cli@latest generate --output src/server/db/schemas/public/auth.ts -y` ### Current vs. Expected behavior instead of ```ts id: int("id").autoincrement.primaryKey(), userId: serial('user_id').primaryKey().notNull().references(()=> user.id, { onDelete: 'cascade' }), ``` I expect ```ts id: serial('id').primaryKey(), userId: integer('user_id').notNull().references(()=> user.id, { onDelete: 'cascade' }), ``` ### What version of Better Auth are you using? 1.2.12 ### Provide environment information ```bash - OS: Nixos ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 08:42:27 -05:00
Author
Owner

@body20002 commented on GitHub (Jul 8, 2025):

it seems that this issue has been resolved in the betaversion
Using
npx @better-auth/cli@beta generate --output src/server/db/schemas/public/auth.ts -y
solve the issue and generate the correct schema.

@body20002 commented on GitHub (Jul 8, 2025): it seems that this issue has been resolved in the `beta`version Using `npx @better-auth/cli@beta generate --output src/server/db/schemas/public/auth.ts -y` solve the issue and generate the correct schema.
Author
Owner

@body20002 commented on GitHub (Jul 8, 2025):

well it doesn't really work since it generate 2 primary keys :/

export const account = pgTable("account", {
  id: serial("id").primaryKey(),
  accountId: text("account_id").notNull(),
  providerId: text("provider_id").notNull(),
  userId: serial("user_id")
    .primaryKey() // <-- this is wrong!
    .notNull()
    .references(() => user.id, { onDelete: "cascade" }),
  accessToken: text("access_token"),
  refreshToken: text("refresh_token"),
  idToken: text("id_token"),
  accessTokenExpiresAt: timestamp("access_token_expires_at"),
  refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
  scope: text("scope"),
  password: text("password"),
  createdAt: timestamp("created_at").notNull(),
  updatedAt: timestamp("updated_at").notNull(),
});
@body20002 commented on GitHub (Jul 8, 2025): well it doesn't really work since it generate 2 primary keys :/ ```ts export const account = pgTable("account", { id: serial("id").primaryKey(), accountId: text("account_id").notNull(), providerId: text("provider_id").notNull(), userId: serial("user_id") .primaryKey() // <-- this is wrong! .notNull() .references(() => user.id, { onDelete: "cascade" }), accessToken: text("access_token"), refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: timestamp("access_token_expires_at"), refreshTokenExpiresAt: timestamp("refresh_token_expires_at"), scope: text("scope"), password: text("password"), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").notNull(), }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1479