[Improvement] Remove redundant index in DB schema (Drizzle) #1166

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

Originally created by @DiiiaZoTe on GitHub (May 6, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Generate the Drizzle schema using the better-auth/cli

Current vs. Expected behavior

The current schema generated for the user table is as follows:

export const user = pgTable("user", {
  id: text("id").primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  emailVerified: boolean('email_verified').notNull(),
  image: text('image'),
  createdAt: timestamp('created_at').notNull(),
  updatedAt: timestamp('updated_at').notNull(),
}, (t) => [
  index('user_email_idx').on(t.email)
]);

The explicitly defined user_email_idx index here is redundant.
The email column is being defined as unique (email TEXT UNIQUE NOT NULL) which will automatically create a unique btree index on the column.
The above is for postgresql but the same applies for mysql and sqlite.

It's really not an issue but it's also unnecessary overhead.

What version of Better Auth are you using?

1.2.7

Provide environment information

- Environment does not matter here

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

Backend

Auth config (if applicable)

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

Additional context

No response

Originally created by @DiiiaZoTe on GitHub (May 6, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Generate the Drizzle schema using the better-auth/cli ### Current vs. Expected behavior The current schema generated for the `user` table is as follows: ``` export const user = pgTable("user", { id: text("id").primaryKey(), name: text('name').notNull(), email: text('email').notNull().unique(), emailVerified: boolean('email_verified').notNull(), image: text('image'), createdAt: timestamp('created_at').notNull(), updatedAt: timestamp('updated_at').notNull(), }, (t) => [ index('user_email_idx').on(t.email) ]); ``` The explicitly defined `user_email_idx` index here is redundant. The email column is being defined as unique (`email TEXT UNIQUE NOT NULL`) which will automatically create a unique btree index on the column. The above is for postgresql but the same applies for mysql and sqlite. It's really not an issue but it's also unnecessary overhead. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - Environment does not matter here ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
Author
Owner

@MuntasirSZN commented on GitHub (May 13, 2025):

I generated with drizzle and user table doesn't have any index at all. Also, as documented, currently the schema generation doesn't add indexing, but is for the future.

Also auth config isn't valid.

@MuntasirSZN commented on GitHub (May 13, 2025): I generated with drizzle and user table doesn't have any `index` at all. Also, as documented, currently the schema generation doesn't add indexing, but is for the future. Also auth config isn't valid.
Author
Owner

@DiiiaZoTe commented on GitHub (May 13, 2025):

Weird idk why mine had it... I guess in that case I'll close this.

@DiiiaZoTe commented on GitHub (May 13, 2025): Weird idk why mine had it... I guess in that case I'll close this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1166