Is it possible to generate a default value concatenating with NEXTVAL?

For example, I want to do this
number: varchar({ length: 15 }).default(`'S' || NEXTVAL('number_sequence')`),
number: varchar({ length: 15 }).default(`'S' || NEXTVAL('number_sequence')`),
But the generated SQL is incorrect with extra quotes wrapping the default:
ALTER TABLE "sessions" ADD COLUMN "number" varchar(15) DEFAULT ''S' || NEXTVAL('session_number_sequence')';
ALTER TABLE "sessions" ADD COLUMN "number" varchar(15) DEFAULT ''S' || NEXTVAL('session_number_sequence')';
1 Reply
wesp
wespOP2mo ago
It looks like this is possible via the sql operator
number: varchar({ length: 15 }).default(sql`'S' || NEXTVAL('${sessionNumberSequence.seqName}')`),
number: varchar({ length: 15 }).default(sql`'S' || NEXTVAL('${sessionNumberSequence.seqName}')`),
But if I try to inject the sequence name I get the following error from drizzle-kit generate
Error: we don't support params for `sql` default values
Error: we don't support params for `sql` default values
It seems this can be done with sql.raw
function generateNumber(prefix: string, sequence: PgSequence) {
return sql.raw(`'${prefix}' || NEXTVAL('${sequence.seqName}')`);
}
function generateNumber(prefix: string, sequence: PgSequence) {
return sql.raw(`'${prefix}' || NEXTVAL('${sequence.seqName}')`);
}
Want results from more Discord servers?
Add your server