how to map custom schema with better-auth
my drizzle tables
eg:
how to map this tables to better-auth user table
this is my config
the error am getting
etterAuthError: The field "name" does not exist in the "user" schema. Please update your drizzle schema or re-generate using "npx @better-auth/cli generate".
export const Users = pgTable(
"users",
{
id: serial("id").primaryKey(),
organizationId: integer("organization_id")
.references(() => Organization.id)
.notNull(),
firstName: varchar("first_name").notNull(),
middleName: varchar("middle_name"),
lastName: varchar("last_name", { length: 55 }).notNull(),
email: text("email").notNull(),
phone: varchar({ length: 15 }),
emailVerified: boolean("email_verified").default(false),
image: text("image"), })
export const Users = pgTable(
"users",
{
id: serial("id").primaryKey(),
organizationId: integer("organization_id")
.references(() => Organization.id)
.notNull(),
firstName: varchar("first_name").notNull(),
middleName: varchar("middle_name"),
lastName: varchar("last_name", { length: 55 }).notNull(),
email: text("email").notNull(),
phone: varchar({ length: 15 }),
emailVerified: boolean("email_verified").default(false),
image: text("image"), })
import * as schema from "../../models";
import { Users } from "../../models/User";
const getAdditionalFields = (schema: PgTable) => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const fields: Record<string, any> = {};
const defaultFields = [
"id",
"email",
"image",
"emailVerified",
"createdAt",
"updatedAt",
];
for (const [key, value] of Object.entries(schema)) {
if (!defaultFields.includes(key)) {
fields[key] = {
type: value.config?.type === "integer" ? "number" : "string",
required: value.notNull ?? false,
};
}
}
return fields;
};
const additionalField = getAdditionalFields(Users);
export const auth = betterAuth({
user: {
additionalFields: additionalField,
},
secret: BETTER_AUTH_SECRET,
baseURL: BETTER_AUTH_URL,
advanced: {
generateId: false,
},
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.Users,
account: schema.Account,
session: schema.Session,
verification: schema.Verification,
organization: schema.Organization,
},
}),
});
import * as schema from "../../models";
import { Users } from "../../models/User";
const getAdditionalFields = (schema: PgTable) => {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const fields: Record<string, any> = {};
const defaultFields = [
"id",
"email",
"image",
"emailVerified",
"createdAt",
"updatedAt",
];
for (const [key, value] of Object.entries(schema)) {
if (!defaultFields.includes(key)) {
fields[key] = {
type: value.config?.type === "integer" ? "number" : "string",
required: value.notNull ?? false,
};
}
}
return fields;
};
const additionalField = getAdditionalFields(Users);
export const auth = betterAuth({
user: {
additionalFields: additionalField,
},
secret: BETTER_AUTH_SECRET,
baseURL: BETTER_AUTH_URL,
advanced: {
generateId: false,
},
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.Users,
account: schema.Account,
session: schema.Session,
verification: schema.Verification,
organization: schema.Organization,
},
}),
});
1 Reply
You need to add
Then compare and add the schema fields as needed
name
to your user table.
It would be helpful if you can generate the schema via the CLI:
npx @better-auth/cli generate
npx @better-auth/cli generate