Error: we don't support params for `sql` default values

I use a specific format for the default value for the primary key for the tables in my PostgreSQL database. In my schema file, this works:
export const unitOfMeasureTable = pgTable("unit_of_measure", {
id: text("id")
.primaryKey()
.notNull()
.default(sql`'uom_' ||replace(gen_random_uuid()::text,'-','')`),
name: text("name").notNull(),
});
export const unitOfMeasureTable = pgTable("unit_of_measure", {
id: text("id")
.primaryKey()
.notNull()
.default(sql`'uom_' ||replace(gen_random_uuid()::text,'-','')`),
name: text("name").notNull(),
});
However, if I create a helper function:
export const primaryKey = (prefix: string) => {
if (prefix.length > 3) throw new TooLongError("prefix", 3);
if (prefix.length === 0) throw new TooShortError("prefix", 1);

const primaryKeyPrefix = `${prefix.toLowerCase()}___`.slice(0, 3);

const primaryKey = sql`${primaryKeyPrefix || ""}||replace(gen_random_uuid()::text,'-','')`;

return primaryKey;
};
export const primaryKey = (prefix: string) => {
if (prefix.length > 3) throw new TooLongError("prefix", 3);
if (prefix.length === 0) throw new TooShortError("prefix", 1);

const primaryKeyPrefix = `${prefix.toLowerCase()}___`.slice(0, 3);

const primaryKey = sql`${primaryKeyPrefix || ""}||replace(gen_random_uuid()::text,'-','')`;

return primaryKey;
};
and change the default .default(primaryKey('uom')) I get Error: we don't support params for 'sql' default values. Is there a way that I can make this work so that I do not need to duplicate the code for each table? Thanks.
1 Reply
Mario564
Mario5642d ago
@RamonaSteve Try using sql.raw instead of sql
Want results from more Discord servers?
Add your server