drizzle-orm: [BUG]: jsonb type on postgres implement incorrectly
What version of drizzle-orm are you using?
0.29.0
What version of drizzle-kit are you using?
0.20.1
Describe the Bug
- create tbl01 in the postgres db
- run this code
import { sql } from "drizzle-orm"
import { jsonb, pgTable, text } from "drizzle-orm/pg-core"
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"
const tbl01 = pgTable("tbl01", {
id: text("id").primaryKey().notNull(),
jsonb_col: jsonb("jsonb_col").notNull(),
})
// CREATE TABLE IF NOT EXISTS public.tbl01
// (
// id text NOT NULL,
// jsonb_col jsonb NOT NULL,
// CONSTRAINT tbl01_pkey PRIMARY KEY (id)
// )
const dbClient = postgres("postgres://postgres:adm1n@winhost:5432/my-data")
const db = drizzle(dbClient, { schema: { tbl01 } })
console.log("INSERT INTO tbl01")
console.log(
await db
.insert(tbl01)
.values({ id: "id01", jsonb_col: { field01: 100, field02: "string 100" } })
.returning()
)
console.log("SELECT QUERY FROM tbl01")
console.log(
await db
.select({
id: tbl01.id,
jsonb_col: tbl01.jsonb_col,
field01: sql`${tbl01.jsonb_col}->>'field01'`, // incorrect: return null, it should be "100"
field02: sql`${tbl01.jsonb_col}->>'field02'`, // incorrect: return null, it should be "string 100"
})
.from(tbl01)
)
console.log("FIND MANY FROM tbl01")
console.log(
await db.query.tbl01.findMany({
columns: {
id: true,
jsonb_col: true,
// how i add field01 and field02 in here ??
},
})
)
Expected behavior
jsonb type incorrectly save data as json string, so we cannot query data field using ->> operator in the postgres.
Environment & setup
No response
About this issue
- Original URL
- State: open
- Created 8 months ago
- Reactions: 2
- Comments: 15
I had a similar issue. To get around the issue I created a customJsonb type where JSON.stringify is skipped:
Drizzle throws a typescript error in the editor but it works and all the JSON functions work, ->> as well.
I did some more checking in the logs of my server and see that Drizzle sends an SQL statement with the following parameter when using the jsonb type provided by Drizzle:
$10 = '"{\"foo\":\"bar\"}"'. Using the customJsonb above, this is:$10 = '{"foo": "bar"}'. I hope the Drizzle team can address this bugI’ve wrote a helper function to deal with nested Jsonb fields in a type-safe way. Maybe it can be helpful for someone.
You can use it like this:
All the arguments will be type safe.
(this assumes that you defined the type of the jsonb field in the schema declaration using the
$typefunction.Hello @primadi,
Regarding your second question :
You can add an
extraskey to thefindManyobject:Though I’m having the same issue regarding the jsonb accessing keys…