define a jsonb column with `{}` as default value does not work

I'm trying to define a jsonb column with default value like this:
attrs: pg
.jsonb('attrs')
.$type<Record<string, JSONValue>>()
.default(sql`'{}'::jsonb`),
attrs: pg
.jsonb('attrs')
.$type<Record<string, JSONValue>>()
.default(sql`'{}'::jsonb`),
however, the inferred type definition of attrs still be Record<string, JSONValue> | null. How to get rid of the null?
Solution:
``ts attrs: pg .jsonb('attrs') .$type<Record<string, JSONValue>>() .default(sql'{}'::jsonb`).notNull(),...
Jump to solution
3 Replies
Angelelz
Angelelz12mo ago
What if you add .notNull()?
Solution
Angelelz
Angelelz12mo ago
attrs: pg
.jsonb('attrs')
.$type<Record<string, JSONValue>>()
.default(sql`'{}'::jsonb`).notNull(),
attrs: pg
.jsonb('attrs')
.$type<Record<string, JSONValue>>()
.default(sql`'{}'::jsonb`).notNull(),
nightire
nightireOP12mo ago
@Angelelz Thank you, it works, I don't expect that easy.
Want results from more Discord servers?
Add your server