Best way to query jsonb field
I can't find any relevent thread other than this one (https://discord.com/channels/1043890932593987624/1164542422328553502)
if i have a column like:
what is the best way to find a value where b === 'test'?
does this query work/make sense?
14 Replies
this doesnt seem to work. not sure how to get a nested field value like b in an
eq
operatorThere is an open issue for jsonb in postgres, drizzle currently is not handling correctly. there's an open PR but it hasn't been merged yet
This is the issue to follow: you'll find workarounds there https://github.com/drizzle-team/drizzle-orm/issues/724
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...
this seems to only reference inserts, not queries
BTW, you should use the
->>
operator and others to access fields in a jsonb columnso my example, i would just update the query clause to be like this right?
is that correct?
I don't really use jsonb columns too much. This page should be helpful https://www.postgresql.org/docs/9.5/functions-json.html
PostgreSQL Documentation
JSON Functions and Operators
i would expect the ORM to have better support for querying a json field type since the ORM already knows the type if i supply
$type
You can currently make it work manually like this, but you are more than welcome to submit a pr for better ergonomics
https://github.com/drizzle-team/drizzle-orm/issues/1690 i submitted this issue. please lmk if anything i wrote is inaccurate
GitHub
[FEATURE]: Native PG jsonb query support · Issue #1690 · drizzle-te...
Describe what you want If I have a column with a jsonb type, and I supply $type, there should be a native way for me to query this field without having to use the sql operator and rawdog the SQL he...
if the issue looks good as written, i will clsoe this post
@Angelelz this doesnt seem to work properly. it returns []
for this row in the DB
My experience with jsonb is kinda limited. I don't like that data type to be honest with you. I prefer the good old relations to put my data in
If you added that data with drizzle, might be why you have problems accessing it
yeah i just spent way too long with this issue as well. if you didnt insert the data as sql
${data}::jsonb
then its a stringified object in the db and your json function wont work. for example see here. The last record i inserted correctly casting it to jsonb manually
@Andrew Sherman hate to tag you but this would be massively appreciated to get merged from @Angelelz
https://github.com/drizzle-team/drizzle-orm/pull/1785GitHub
Fix: json and jsonb parsing in postgres-js by Angelelz · Pull Reque...
Second (and better) attempt to close #724 and close #1511
This PR depends on #1659 which has already been merged to beta.
After figuring out how to bypass parsers and serializers in postgres.js dri...
yeah, we will add it, but would need to change a logic a bit