Postgrest inserting json array results in stringified array

I'm trying to insert a an array of objects as a json array but it always ends up being strinfied if it would be just a json then i can do sql${recipeData.notes}::jsonb` but that doesn't work with arrays (cannot cast type record to jsonb`)
4 Replies
hugo
hugo7mo ago
hey! I noticed the same issue, having a jsonb doesn't save the array, but just a string instead. I have my column defined like this:
readByJson: jsonb('read_by').$type<string[]>(),
readByJson: jsonb('read_by').$type<string[]>(),
any way to make it save an actual array instead of the string?
hugo
hugo7mo ago
GitHub
[BUG]: jsonb always inserted as a json string when using postgres-j...
What version of drizzle-orm are you using? 0.26.5 What version of drizzle-kit are you using? 0.18.1 Describe the Bug Inserting an object into a postgres jsonb field with db.insert only inserts a st...
Amur
Amur7mo ago
i ended up using a custom json field
export const customJsonb = <TData>(name: string) =>
customType<{ data: TData; driverData: string }>({
dataType() {
return "jsonb";
},
// @ts-expect-error https://github.com/drizzle-team/drizzle-orm/issues/1511#issuecomment-1824687669
toDriver(value: TData) {
return value;
},
})(name);

// schema
notes: customJsonb<Array<RecipeNote>>("notes"),
export const customJsonb = <TData>(name: string) =>
customType<{ data: TData; driverData: string }>({
dataType() {
return "jsonb";
},
// @ts-expect-error https://github.com/drizzle-team/drizzle-orm/issues/1511#issuecomment-1824687669
toDriver(value: TData) {
return value;
},
})(name);

// schema
notes: customJsonb<Array<RecipeNote>>("notes"),
hugo
hugo7mo ago
yeah that works or use jsonb objects instead if arrays
Want results from more Discord servers?
Add your server