[GH-ISSUE #4189] "BetterAuthError: The field "username" does not exist in the schema" after setting up a new project #9857

Closed
opened 2026-04-13 05:38:04 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @amlcx on GitHub (Aug 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/4189

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Toolchain I'm using:

  • Bun
  • SvelteKit
  • BetterAuth
  • Drizzle

To reproduce the error, all I have to do is simply follow the official guide after creating a new SvelteKit project.

Current vs. Expected behavior

Whenever I try to do anything auth-related, my app crashes (even the process is terminated) and the error message I see in my terminal is:

[BetterAuthError: The field "username" does not exist in the schema for the model "user". Please update your schema.] {
  cause: undefined
}

Which shouldn't be the case, because this is the table in Postgres after running the Drizzle migration:

Table "public.user"
      Column      |            Type             | Collation | Nullable | Default 
------------------+-----------------------------+-----------+----------+---------
 id               | text                        |           | not null | 
 name             | text                        |           | not null | 
 email            | text                        |           | not null | 
 email_verified   | boolean                     |           | not null | 
 image            | text                        |           |          | 
 created_at       | timestamp without time zone |           | not null | 
 updated_at       | timestamp without time zone |           | not null | 
 username         | text                        |           |          | 
 display_username | text                        |           |          | 
 role             | text                        |           |          | 
 banned           | boolean                     |           |          | 
 ban_reason       | text                        |           |          | 
 ban_expires      | timestamp without time zone |           |          | 
Indexes:
    "user_pkey" PRIMARY KEY, btree (id)
    "user_email_unique" UNIQUE CONSTRAINT, btree (email)
    "user_username_unique" UNIQUE CONSTRAINT, btree (username)
Referenced by:
    TABLE "account" CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "category" CONSTRAINT "category_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "comment" CONSTRAINT "comment_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "media_user" CONSTRAINT "media_user_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "poll_vote" CONSTRAINT "poll_vote_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "session" CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
    TABLE "thread" CONSTRAINT "thread_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE

I have another local project that was working fine just until yesterday morning (the 22nd), and the only difference I notice is the bump from ^1.3.6 to ^1.3.7.

What version of Better Auth are you using?

1.3.7

System info

System:
    OS: Linux 6.15 Fedora Linux 42 (KDE Plasma Desktop Edition)
    CPU: (12) x64 AMD Ryzen 5 5600U with Radeon Graphics
    Memory: 2.21 GB / 7.10 GB
    Container: Yes
    Shell: 5.2.37 - /bin/bash

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

Backend, Client

Auth config (if applicable)

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

Additional context

No response

Originally created by @amlcx on GitHub (Aug 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/4189 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Toolchain I'm using: - Bun - SvelteKit - BetterAuth - Drizzle To reproduce the error, all I have to do is simply follow the official guide after creating a new SvelteKit project. ### Current vs. Expected behavior Whenever I try to do anything auth-related, my app crashes (even the process is terminated) and the error message I see in my terminal is: ``` [BetterAuthError: The field "username" does not exist in the schema for the model "user". Please update your schema.] { cause: undefined } ``` Which shouldn't be the case, because this is the table in Postgres after running the Drizzle migration: ``` Table "public.user" Column | Type | Collation | Nullable | Default ------------------+-----------------------------+-----------+----------+--------- id | text | | not null | name | text | | not null | email | text | | not null | email_verified | boolean | | not null | image | text | | | created_at | timestamp without time zone | | not null | updated_at | timestamp without time zone | | not null | username | text | | | display_username | text | | | role | text | | | banned | boolean | | | ban_reason | text | | | ban_expires | timestamp without time zone | | | Indexes: "user_pkey" PRIMARY KEY, btree (id) "user_email_unique" UNIQUE CONSTRAINT, btree (email) "user_username_unique" UNIQUE CONSTRAINT, btree (username) Referenced by: TABLE "account" CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE TABLE "category" CONSTRAINT "category_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE TABLE "comment" CONSTRAINT "comment_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE TABLE "media_user" CONSTRAINT "media_user_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE TABLE "poll_vote" CONSTRAINT "poll_vote_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE TABLE "session" CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE TABLE "thread" CONSTRAINT "thread_created_by_user_id_fk" FOREIGN KEY (created_by) REFERENCES "user"(id) ON DELETE CASCADE ``` I have another local project that was working fine just until yesterday morning (the 22nd), and the only difference I notice is the bump from ^1.3.6 to ^1.3.7. ### What version of Better Auth are you using? 1.3.7 ### System info ```bash System: OS: Linux 6.15 Fedora Linux 42 (KDE Plasma Desktop Edition) CPU: (12) x64 AMD Ryzen 5 5600U with Radeon Graphics Memory: 2.21 GB / 7.10 GB Container: Yes Shell: 5.2.37 - /bin/bash ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 05:38:04 -05:00
Author
Owner

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

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

This is a known issue in BetterAuth 1.3.7 when using Drizzle: the username plugin expects camelCase field names like displayUsername, but your database schema (and Drizzle model) uses snake_case like display_username. The plugin does not respect field mapping, so it tries to access a column that doesn't exist, causing the error you see. This was fixed in a later version (see PR #3021), so upgrading BetterAuth is the best solution if possible.
Source

If you need a workaround for 1.3.7, you can monkey-patch the username plugin to rewrite displayUsername to display_username before DB operations. Here’s how:

import { username } from "better-auth/plugins";

// Patch the username plugin's database hooks
function patchedUsername(options) {
  const orig = username(options);
  const origInit = orig.init;
  orig.init = (ctx) => {
    const plugin = origInit(ctx);
    const hooks = plugin.options?.databaseHooks?.user;
    if (hooks) {
      ["create", "update"].forEach((op) => {
        if (hooks[op]?.before) {
          const origBefore = hooks[op].before;
          hooks[op].before = async (user, context) => {
            if ("displayUsername" in user) {
              user.display_username = user.displayUsername;
              delete user.displayUsername;
            }
            return origBefore(user, context);
          };
        }
      });
    }
    return plugin;
  };
  return orig;
}

// Use patchedUsername() in your plugins array instead of username()

Source & explanation

You can remove this patch once you upgrade to a version where the field mapping is handled automatically.

If you’re not intentionally using the username plugin, double-check your config and build output to make sure it’s not being included by accident—BetterAuth only requires the username field if the plugin is enabled in your config.
Debugging tips

To reply, just mention @dosu.


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

<!-- gh-comment-id:3217785548 --> @dosubot[bot] commented on GitHub (Aug 24, 2025): <!-- Greeting --> Hi @amlcx! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known issue in BetterAuth 1.3.7 when using Drizzle: the username plugin expects camelCase field names like `displayUsername`, but your database schema (and Drizzle model) uses snake_case like `display_username`. The plugin does not respect field mapping, so it tries to access a column that doesn't exist, causing the error you see. This was fixed in a later version (see PR #3021), so upgrading BetterAuth is the best solution if possible. [Source](https://github.com/better-auth/better-auth/issues/4066#issuecomment-3196300909) If you need a workaround for 1.3.7, you can monkey-patch the username plugin to rewrite `displayUsername` to `display_username` before DB operations. Here’s how: ```js import { username } from "better-auth/plugins"; // Patch the username plugin's database hooks function patchedUsername(options) { const orig = username(options); const origInit = orig.init; orig.init = (ctx) => { const plugin = origInit(ctx); const hooks = plugin.options?.databaseHooks?.user; if (hooks) { ["create", "update"].forEach((op) => { if (hooks[op]?.before) { const origBefore = hooks[op].before; hooks[op].before = async (user, context) => { if ("displayUsername" in user) { user.display_username = user.displayUsername; delete user.displayUsername; } return origBefore(user, context); }; } }); } return plugin; }; return orig; } // Use patchedUsername() in your plugins array instead of username() ``` [Source & explanation](https://github.com/better-auth/better-auth/issues/4066#issuecomment-3196533781) You can remove this patch once you upgrade to a version where the field mapping is handled automatically. If you’re not intentionally using the username plugin, double-check your config and build output to make sure it’s not being included by accident—BetterAuth only requires the `username` field if the plugin is enabled in your config. [Debugging tips](https://github.com/better-auth/better-auth/discussions/3387#discussioncomment-13764319) <!-- 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/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/36d38153-030d-4f60-9c7b-521af523a1c6?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/4189)
Author
Owner

@iamsg97 commented on GitHub (Aug 24, 2025):

So this is fixed, and needs no working? @Kinfe123

<!-- gh-comment-id:3217863690 --> @iamsg97 commented on GitHub (Aug 24, 2025): So this is fixed, and needs no working? @Kinfe123
Author
Owner

@Kinfe123 commented on GitHub (Aug 24, 2025):

What does the full auth config looks like ?

<!-- gh-comment-id:3218245288 --> @Kinfe123 commented on GitHub (Aug 24, 2025): What does the full auth config looks like ?
Author
Owner

@iamsg97 commented on GitHub (Aug 24, 2025):

No, I did not solve it. I just read the dosubot comment. It appears to be something that the consumer can do. Is it still in scope is what I was wondering!

<!-- gh-comment-id:3218274472 --> @iamsg97 commented on GitHub (Aug 24, 2025): No, I did not solve it. I just read the `dosubot` comment. It appears to be something that the consumer can do. Is it still in scope is what I was wondering!
Author
Owner

@Kinfe123 commented on GitHub (Aug 24, 2025):

Yeah for that. That would be helpful if you share auth config which helps to reproduce the issue and see if it something that needs a fix from ourside please ?

<!-- gh-comment-id:3218275679 --> @Kinfe123 commented on GitHub (Aug 24, 2025): Yeah for that. That would be helpful if you share auth config which helps to reproduce the issue and see if it something that needs a fix from ourside please ?
Author
Owner

@amlcx commented on GitHub (Aug 25, 2025):

Hi! @Kinfe123

Here's my auth.ts:

import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { db } from './server/db';
import { admin, jwt, username } from 'better-auth/plugins';
import { sveltekitCookies } from 'better-auth/svelte-kit';
import { getRequestEvent } from '$app/server';

export const auth = betterAuth({
	database: drizzleAdapter(db, {
		provider: 'pg',
		usePlural: true,
	}),

	emailAndPassword: {
		enabled: true
	},

	plugins: [
		username(),
		admin(),
		jwt({
			jwt: {
				definePayload: ({ user }) => {
					return {
						id: user.id,
						username: user.username,
						role: user.role
					};
				}
			}
		}),
		sveltekitCookies(getRequestEvent)
	],

	baseURL: 'http://localhost:5173'
});

But as of now, it's working again for me, after adding usePlural in the drizzleAdapter config. If you think this is something we should explore further, please let me know; otherwise, I think is okay to mark this as closed.

<!-- gh-comment-id:3218738760 --> @amlcx commented on GitHub (Aug 25, 2025): Hi! @Kinfe123 Here's my `auth.ts`: ```typescript import { betterAuth } from 'better-auth'; import { drizzleAdapter } from 'better-auth/adapters/drizzle'; import { db } from './server/db'; import { admin, jwt, username } from 'better-auth/plugins'; import { sveltekitCookies } from 'better-auth/svelte-kit'; import { getRequestEvent } from '$app/server'; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg', usePlural: true, }), emailAndPassword: { enabled: true }, plugins: [ username(), admin(), jwt({ jwt: { definePayload: ({ user }) => { return { id: user.id, username: user.username, role: user.role }; } } }), sveltekitCookies(getRequestEvent) ], baseURL: 'http://localhost:5173' }); ``` But as of now, it's working again for me, after adding usePlural in the drizzleAdapter config. If you think this is something we should explore further, please let me know; otherwise, I think is okay to mark this as closed.
Author
Owner

@amlcx commented on GitHub (Sep 6, 2025):

Coming back to this, I'll close the issue, since it seems like no one else is facing a similar error. Thanks to kinfe and iamsg for helping me!

<!-- gh-comment-id:3260299289 --> @amlcx commented on GitHub (Sep 6, 2025): Coming back to this, I'll close the issue, since it seems like no one else is facing a similar error. Thanks to kinfe and iamsg for helping me!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9857