🆘 Field mapping issue with organization plugin: 'userId does not exist in schema for model member

Hello Better Auth community 👋 I'm having an issue with the organization plugin and field mapping. When trying to create an organization, I get this error:
[BetterAuthError: The field "userId" does not exist in the schema for the model "member". Please update your schema.]
[BetterAuthError: The field "userId" does not exist in the schema for the model "member". Please update your schema.]
My setup: - Using Better Auth with Drizzle adapter - Database fields use snake_case (user_id, organization_id, etc.) - Organization plugin configured with proper field mappings I've tried: 1. Adding field mappings at the root level in my betterAuth() config:
member: {
modelName: "member",
fields: {
userId: "user_id",
organizationId: "organization_id",
teamId: "team_id"
}
}
member: {
modelName: "member",
fields: {
userId: "user_id",
organizationId: "organization_id",
teamId: "team_id"
}
}
2. Adding schema mappings in the organization plugin config:
organization({
// Other options...
schema: {
member: {
fields: {
userId: "user_id",
organizationId: "organization_id",
teamId: "team_id"
}
}
}
})
organization({
// Other options...
schema: {
member: {
fields: {
userId: "user_id",
organizationId: "organization_id",
teamId: "team_id"
}
}
}
})
But I still get the same error. What's the proper way to map camelCase field names to snake_case database columns for the organization plugin? Any help would be appreciated! Thanks!
No description
No description
12 Replies
Jackson Kasi
Jackson KasiOP•4mo ago
@admin plz help. it's bit urgent
Blank
Blank•4mo ago
did you pass in the schema to better auth? show your auth config
Jackson Kasi
Jackson KasiOP•4mo ago
yes i passed. let me share the code
Jackson Kasi
Jackson KasiOP•4mo ago
Gist
🆘 Field mapping issue with organization plugin: 'userId does not...
🆘 Field mapping issue with organization plugin: 'userId does not exist in schema for model member - packages\better-auth\README.md
Blank
Blank•4mo ago
have you run the migrations? the code looks fine
Jackson Kasi
Jackson KasiOP•4mo ago
Should I use Drizzle migration or BetterAuth CLI migration? If it's BetterAuth CLI, does it support Turborepo? Because both the database and BetterAuth are managed as separate packages
Blank
Blank•4mo ago
drizzle one
Jackson Kasi
Jackson KasiOP•4mo ago
Hi @Blank Earlier, I used drizzle-kit push. Just now, I created a new database and this time used drizzle-kit migrate:
$ pnpm db:migrate

> @repo/db@1.0.0 db:migrate D:\WORK\PEACOCK\Patient-Lens-AI\patient-lens-ai\packages\db
> dotenv -e .env drizzle-kit migrate

No config path provided, using default 'drizzle.config.ts'
Reading config file 'D:\WORK\PEACOCK\Patient-Lens-AI\patient-lens-ai\packages\db\drizzle.config.ts'
Using 'postgres' driver for database querying
[✓] Migrations applied successfully!
$ pnpm db:migrate

> @repo/db@1.0.0 db:migrate D:\WORK\PEACOCK\Patient-Lens-AI\patient-lens-ai\packages\db
> dotenv -e .env drizzle-kit migrate

No config path provided, using default 'drizzle.config.ts'
Reading config file 'D:\WORK\PEACOCK\Patient-Lens-AI\patient-lens-ai\packages\db\drizzle.config.ts'
Using 'postgres' driver for database querying
[✓] Migrations applied successfully!
Then, I created an account and tried to create an organization, but I got the same error:
<-- POST /api/auth/organization/create
X [ERROR] # SERVER_ERROR: [BetterAuthError: The field "userId" does not exist in the schema for the model "member". Please update your schema.] {
cause: undefined
}
--> POST /api/auth/organization/create 500 196ms
[wrangler:inf] POST /api/auth/organization/create 500 Internal Server Error (199ms)
<-- POST /api/auth/organization/create
X [ERROR] # SERVER_ERROR: [BetterAuthError: The field "userId" does not exist in the schema for the model "member". Please update your schema.] {
cause: undefined
}
--> POST /api/auth/organization/create 500 196ms
[wrangler:inf] POST /api/auth/organization/create 500 Internal Server Error (199ms)
Any suggestions?
Ping
Ping•4mo ago
Try changing your schema to have the variable with how BA wants it, but within the drizzle field definiton, keep it the way you had it, here's an example:
xport const tbl_member = pgTable("member", {
// Identity
id: text("id").primaryKey().notNull(),

// Relations
userId: text("user_id")
.notNull()
.references(() => tbl_user.id, { onDelete: "cascade" }),

organizationId: text("organization_id")
.notNull()
.references(() => tbl_organization.id, { onDelete: "cascade" }),

teamId: text("team_id").references(() => tbl_team.id, {
onDelete: "set null",
}),

// Role & Access
role: text("role", { enum: MEMBER_ROLES })
.notNull()
.$defaultFn(() => "member"),

// Lifecycle timestamps (always at the bottom)
...lifecycleDates,
});
xport const tbl_member = pgTable("member", {
// Identity
id: text("id").primaryKey().notNull(),

// Relations
userId: text("user_id")
.notNull()
.references(() => tbl_user.id, { onDelete: "cascade" }),

organizationId: text("organization_id")
.notNull()
.references(() => tbl_organization.id, { onDelete: "cascade" }),

teamId: text("team_id").references(() => tbl_team.id, {
onDelete: "set null",
}),

// Role & Access
role: text("role", { enum: MEMBER_ROLES })
.notNull()
.$defaultFn(() => "member"),

// Lifecycle timestamps (always at the bottom)
...lifecycleDates,
});
Jackson Kasi
Jackson KasiOP•4mo ago
Hi @Ping, thanks for your suggestion. Why doesn't BetterAuth support custom column names? https://www.better-auth.com/docs/concepts/database#custom-table-names
Database | Better Auth
Learn how to use a database with Better Auth.
Ping
Ping•4mo ago
I think it's just for drizzle adapter.
Jackson Kasi
Jackson KasiOP•4mo ago
Thanks for the suggestion @Ping . I'd really like to maintain snake_case throughout my codebase for consistency, not just in the database columns. Is there any way to make Better Auth work with snake_case variable names too? The documentation mentions support for custom field names, so it seems like this should be possible. If I have to use camelCase variables as you suggested, can I at least use something like Zod or a type layer to maintain snake_case in my application code while just having the camelCase in the schema definition?

Did you find this page helpful?