You can't put an empty array as a defaultValue for an additionalField in the user schema

My observation is simple, I can create an additional field of type string[], I can use the property defaultValue to [], and it will generate a .default() in my drizzle schema once generated by the Better-Auth CLI. But better-auth forgets to add the [] as a parameter of the .default() so I get a drizzle schema that says "this field should have a default value !" but doesn't provide that value. Also, according to Drizzle's documentation (https://orm.drizzle.team/docs/guides/empty-array-default-value#postgresql) the correct way to put an empty array as a default value of an array field is to use sql templates. Here's the example given by Drizzle's documentation:
export const users = pgTable('users', {
tags: text('tags')
.array()
.notNull()
.default(sql`'{}'::text[]`),
});
export const users = pgTable('users', {
tags: text('tags')
.array()
.notNull()
.default(sql`'{}'::text[]`),
});
If you don't do it that way, the migration will not put an empty array as a default value, so whenever you create a new record, this field of the record will be empty, which means that if you try to push an item to the array or empty the array, it will cause an error.
Drizzle ORM - Empty array as a default value
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
1 Reply
Filou
FilouOP2w ago
It seems that the prisma generator handles it correctly:
if (attr.defaultValue.length === 0) {
fieldBuilder.attribute(`default([])`); continue;
}
if (attr.defaultValue.length === 0) {
fieldBuilder.attribute(`default([])`); continue;
}

Did you find this page helpful?