Customizing core tables user, session, names generates bad migration schemas #1229

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

Originally created by @changalberto on GitHub (May 18, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Renaming the core user, and session table name cause the cli to generate bad schemas. The other core tables, account, verification are unaffected by the model name change.

And here's the generated migrations:

alter table "users" add column "name" text not null;

alter table "users" add column "email_verified" boolean not null;

alter table "users" add column "image" text;

alter table "users" add column "phone_number" text unique;

alter table "users" add column "phone_number_verified" boolean;

alter table "sessions" add column "expires_at" timestamp not null;

alter table "sessions" add column "token" text not null unique;

alter table "sessions" add column "ip_address" text;

create table "accounts" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "users" ("id"), "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null);

create table "verifications" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp, "updated_at" timestamp);

create table "jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamp not null);

As you can see, the migration doesn't create a table for users or sessions as compared to accounts and verifications tables

As a nice to have, would be nice to have a single configuration to name table fields to use different name convention rather than having to define each individually. Perhaps a option such as useSnakeCasing: true

Current vs. Expected behavior

Current behavior generates an alter users, sessions table query

Expected behavior is to generate a create users, sessions table query

What version of Better Auth are you using?

Latest

Provide environment information

- OS: 15.4.1

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

Package

Auth config (if applicable)

import { betterAuth } from "better-auth"

export const auth = betterAuth({
  adapter: drizzleAdapter(db, {
    provider: 'pg', // PostgreSQL
    schema: {
      user: users,
      session: sessions,
    },
    usePlural: true, // Explicitly enforce plural naming convention
  }),
  database: new Pool({
    connectionString: env.DATABASE_URL_DIRECT,
  }),
  user: {
    modelName: 'users',
    fields: {
      emailVerified: 'email_verified',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    },
  },
  session: {
    modelName: 'sessions',
    fields: {
      userId: 'user_id',
      expiresAt: 'expires_at',
      ipAddress: 'ip_address',
      userAgent: 'user_agent',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    },
  },
  account: {
    modelName: 'accounts',
    fields: {
      userId: 'user_id',
      accountId: 'account_id',
      providerId: 'provider_id',
      accessToken: 'access_token',
      refreshToken: 'refresh_token',
      accessTokenExpiresAt: 'access_token_expires_at',
      refreshTokenExpiresAt: 'refresh_token_expires_at',
      idToken: 'id_token',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    },
  },
  verification: {
    modelName: 'verifications',
    fields: {
      expiresAt: 'expires_at',
      createdAt: 'created_at',
      updatedAt: 'updated_at',
    },
  },
  plugins: [
    phoneNumber({
      schema: {
        user: {
          fields: {
            phoneNumber: 'phone_number',
            phoneNumberVerified: 'phone_number_verified',
          },
        },
      },
    // Adding the JWT plugin using the proper nest structure.
    jwt({
      schema: {
        jwks: {
          fields: {
            publicKey: 'public_key',
            privateKey: 'private_key',
            createdAt: 'created_at',
          },
        },
      },
  ],
});

Additional context

bunx @better-auth/cli@latest generate
and
bunx @better-auth/cli@beta generate

Originally created by @changalberto on GitHub (May 18, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Renaming the core user, and session table name cause the cli to generate bad schemas. The other core tables, account, verification are unaffected by the model name change. And here's the generated migrations: ```sql alter table "users" add column "name" text not null; alter table "users" add column "email_verified" boolean not null; alter table "users" add column "image" text; alter table "users" add column "phone_number" text unique; alter table "users" add column "phone_number_verified" boolean; alter table "sessions" add column "expires_at" timestamp not null; alter table "sessions" add column "token" text not null unique; alter table "sessions" add column "ip_address" text; create table "accounts" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "users" ("id"), "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null); create table "verifications" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp, "updated_at" timestamp); create table "jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamp not null); ``` As you can see, the migration doesn't create a table for `users` or `sessions` as compared to `accounts` and `verifications` tables As a nice to have, would be nice to have a single configuration to name table fields to use different name convention rather than having to define each individually. Perhaps a option such as `useSnakeCasing: true` ### Current vs. Expected behavior Current behavior generates an alter users, sessions table query Expected behavior is to generate a create users, sessions table query ### What version of Better Auth are you using? Latest ### Provide environment information ```bash - OS: 15.4.1 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ adapter: drizzleAdapter(db, { provider: 'pg', // PostgreSQL schema: { user: users, session: sessions, }, usePlural: true, // Explicitly enforce plural naming convention }), database: new Pool({ connectionString: env.DATABASE_URL_DIRECT, }), user: { modelName: 'users', fields: { emailVerified: 'email_verified', createdAt: 'created_at', updatedAt: 'updated_at', }, }, session: { modelName: 'sessions', fields: { userId: 'user_id', expiresAt: 'expires_at', ipAddress: 'ip_address', userAgent: 'user_agent', createdAt: 'created_at', updatedAt: 'updated_at', }, }, account: { modelName: 'accounts', fields: { userId: 'user_id', accountId: 'account_id', providerId: 'provider_id', accessToken: 'access_token', refreshToken: 'refresh_token', accessTokenExpiresAt: 'access_token_expires_at', refreshTokenExpiresAt: 'refresh_token_expires_at', idToken: 'id_token', createdAt: 'created_at', updatedAt: 'updated_at', }, }, verification: { modelName: 'verifications', fields: { expiresAt: 'expires_at', createdAt: 'created_at', updatedAt: 'updated_at', }, }, plugins: [ phoneNumber({ schema: { user: { fields: { phoneNumber: 'phone_number', phoneNumberVerified: 'phone_number_verified', }, }, }, // Adding the JWT plugin using the proper nest structure. jwt({ schema: { jwks: { fields: { publicKey: 'public_key', privateKey: 'private_key', createdAt: 'created_at', }, }, }, ], }); ``` ### Additional context `bunx @better-auth/cli@latest generate` and `bunx @better-auth/cli@beta generate`
Author
Owner

@ctrhub commented on GitHub (Jun 7, 2025):

facing similar issue. additionalFields works fine, but customised fields are ignored

@ctrhub commented on GitHub (Jun 7, 2025): facing similar issue. `additionalFields` works fine, but customised `fields` are ignored
Author
Owner

@dosubot[bot] commented on GitHub (Sep 6, 2025):

Hi, @changalberto. 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 renaming core tables like user and session causes the migration CLI to alter existing tables incorrectly instead of creating new ones.
  • This behavior differs from other core tables and happens on the latest version with OS 15.4.1.
  • Another user, ctrhub, mentioned a related issue where customized fields are ignored, though additionalFields work fine.
  • The issue remains unresolved with no recent updates or fixes.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back within 7 days, this issue will be automatically closed.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Sep 6, 2025): Hi, @changalberto. 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 renaming core tables like user and session causes the migration CLI to alter existing tables incorrectly instead of creating new ones. - This behavior differs from other core tables and happens on the latest version with OS 15.4.1. - Another user, ctrhub, mentioned a related issue where customized fields are ignored, though additionalFields work fine. - The issue remains unresolved with no recent updates or fixes. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back within 7 days, this issue will be automatically closed. 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#1229