{{ message }}
How to SELECT using JSON for WHERE-clause #2023
Answered
by
apoorva-01
brainbytes42
asked this question in
Questions
Replies: 1 comment
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

.as_string()is the right call here, not a workaround. It's the documented SQLAlchemy way and your original attempt is doing something subtly different than you'd expect.When you write
Foo.meta["hello"], the indexed access gives you back a value that's still typed as JSON, not as text. SoFoo.meta["hello"] == "world"is a JSON-to-JSON comparison. On SQLite that renders asJSON_QUOTE(JSON_EXTRACT(meta, '$."hello"')), andJSON_QUOTEwraps the value in quotes, so you're really comparing the string"world"(quotes included) against your bare'world'. They never match, which is why you getNone..as_string()tells SQLAlchemy to pull the element out as text instead of JSON, so it drops theJSO…