Invalid drizzle schema for `defaultValue` of additionalField with type `string[]`

Below is how my src/auth/auth.ts tooks
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
schema: authschema,
}),

// ...

user: {
modelName: "usersTable",
additionalFields: {
roles: { type: "string[]", defaultValue: ["guest"], input: false },
description: { type: "string" },
address: { type: "string" },
},
},
});
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
schema: authschema,
}),

// ...

user: {
modelName: "usersTable",
additionalFields: {
roles: { type: "string[]", defaultValue: ["guest"], input: false },
description: { type: "string" },
address: { type: "string" },
},
},
});
After I run npx @better-auth/cli generate --config src/auth/auth.ts --output src/db/schema/auth.ts -y to generate the corresponding drizzle schema, i get
import {
pgTable,
text,
timestamp,
boolean,
integer,
} from "drizzle-orm/pg-core";

export const usersTable = pgTable("users_table", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified")
.$defaultFn(() => false)
.notNull(),
image: text("image"),
createdAt: timestamp("created_at")
.$defaultFn(() => /* @__PURE__ */ new Date())
.notNull(),
updatedAt: timestamp("updated_at")
.$defaultFn(() => /* @__PURE__ */ new Date())
.notNull(),
roles: text("roles").array().default(guest), // should be ["guest"] instead
description: text("description"),
address: text("address"),
});

// ... other schema
import {
pgTable,
text,
timestamp,
boolean,
integer,
} from "drizzle-orm/pg-core";

export const usersTable = pgTable("users_table", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified")
.$defaultFn(() => false)
.notNull(),
image: text("image"),
createdAt: timestamp("created_at")
.$defaultFn(() => /* @__PURE__ */ new Date())
.notNull(),
updatedAt: timestamp("updated_at")
.$defaultFn(() => /* @__PURE__ */ new Date())
.notNull(),
roles: text("roles").array().default(guest), // should be ["guest"] instead
description: text("description"),
address: text("address"),
});

// ... other schema
this shouldn't be a bug, what am I doing wrong?
Solution:
nevermind
Jump to solution
2 Replies
Solution
mrzoro
mrzoro3mo ago
nevermind
mrzoro
mrzoroOP3mo ago
I looked at @better-auth/cli source and found it takes a function so, now it works for me
roles: { type: "string[]", defaultValue: () => ["guest"], input: false },
roles: { type: "string[]", defaultValue: () => ["guest"], input: false },

Did you find this page helpful?