UUID Confusion with database orm

Hi I just recently switched from Prisma-orm to Drizzle-orm in the project that I am working on. Having a bit of issue where my user table doesn't seem to understand the uuid coming from my sign-up form. Curious to see if anyone knew exactly what I have done wrong?
Solution:
Yes this is completely fine. If you want the old way where your DB handles ID generation, you can disable Better-Auth's ID generation by passing advanced.database.generateId as false in your auth config.
Jump to solution
3 Replies
Jacob | idohtml
Jacob | idohtmlOP5mo ago
My auth.ts file looks something like this.
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";
import { db } from "./database/index";
import * as schema from "./database/schema";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.users,
session: schema.sessions,
acount: schema.accounts,
verification: schema.verifications,
pizza: schema.pizzas,
group: schema.groups,
},
usePlural: true,
}),
secret: process.env.AUTH_SECRET!,
emailAndPassword: {
enabled: true,
autoSignIn: true,
},
plugins: [nextCookies()],
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { nextCookies } from "better-auth/next-js";
import { db } from "./database/index";
import * as schema from "./database/schema";

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.users,
session: schema.sessions,
acount: schema.accounts,
verification: schema.verifications,
pizza: schema.pizzas,
group: schema.groups,
},
usePlural: true,
}),
secret: process.env.AUTH_SECRET!,
emailAndPassword: {
enabled: true,
autoSignIn: true,
},
plugins: [nextCookies()],
});
The user schema looks like this
export const users = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name"),
email: text("email").notNull().unique(),
emailVerified: boolean("emailVerified"),
role: text("role", { enum: ["USER", "ADMIN", "SYSTEMADMIN"] }).default(
"USER",
), // Assuming UserRoles enum had USER and ADMIN
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt")
.defaultNow()
.notNull()
.$onUpdate(() => new Date()),
image: text("image"),
});
export const users = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name"),
email: text("email").notNull().unique(),
emailVerified: boolean("emailVerified"),
role: text("role", { enum: ["USER", "ADMIN", "SYSTEMADMIN"] }).default(
"USER",
), // Assuming UserRoles enum had USER and ADMIN
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt")
.defaultNow()
.notNull()
.$onUpdate(() => new Date()),
image: text("image"),
});
The error I am seeing in my console when trying to sign up a new user is this
2025-05-05T23:10:41.718Z ERROR [Better Auth]: Failed to create user [error: invalid input syntax for type uuid: "CGnU4AfCk6XaHIZSEwM66NlOeIAEMKjb"] {
length: 164,
severity: 'ERROR',
code: '22P02',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: "unnamed portal parameter $1 = '...'",
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'uuid.c',
line: '137',
routine: 'string_to_uuid'
}
POST /api/auth/sign-up/email 422 in 1260ms
2025-05-05T23:10:41.718Z ERROR [Better Auth]: Failed to create user [error: invalid input syntax for type uuid: "CGnU4AfCk6XaHIZSEwM66NlOeIAEMKjb"] {
length: 164,
severity: 'ERROR',
code: '22P02',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: "unnamed portal parameter $1 = '...'",
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'uuid.c',
line: '137',
routine: 'string_to_uuid'
}
POST /api/auth/sign-up/email 422 in 1260ms
I might have solved this myself. I am not sure if this is the correct way of doing it but I deleted my old schema.ts file and ran the npx @better-auth/cli@latest generate cli to get a new schema file. However this gives the id field a text value so that we can pass the NanoID into the database. Is this secure/correct way of doing this? Meaning my user schema looks like this
export const users = pgTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").notNull(),
role: text("role", { enum: ["USER", "ADMIN", "SYSTEMADMIN"] }).default(
"USER",
),
image: text("image"),
createdAt: timestamp("created_at").notNull(),
updatedAt: timestamp("updated_at").notNull(),
});
export const users = pgTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").notNull(),
role: text("role", { enum: ["USER", "ADMIN", "SYSTEMADMIN"] }).default(
"USER",
),
image: text("image"),
createdAt: timestamp("created_at").notNull(),
updatedAt: timestamp("updated_at").notNull(),
});
Solution
Ping
Ping5mo ago
Yes this is completely fine. If you want the old way where your DB handles ID generation, you can disable Better-Auth's ID generation by passing advanced.database.generateId as false in your auth config.
Jacob | idohtml
Jacob | idohtmlOP5mo ago
Which one is the recommended way?

Did you find this page helpful?