This is just a simple example.
field = [[1,12,6], [2,12,8]]
I think about storing this in a json-typed field with SQLAlchemy in sqlite3 database.
But how can I query on this with SQLAlchemy (not SQL)? e.g.
Is it even possible?
Maybe it would be easier to implent this with a classical many-to-many table (incl. a link-table) instead of json-field?
All SELECT statements generated by SQLAlchemy ORM are constructed by Query object. It provides a generative interface, hence successive calls return a new Query object, a copy of the former with additional criteria and options associated with it.
For both Core and ORM, the select() function generates a Select construct which is used for all SELECT queries. Passed to methods like Connection. execute() in Core and Session. execute() in ORM, a SELECT statement is emitted in the current transaction and the result rows available via the returned Result object.
_sa_instance_state is a non-database-persisted value used by SQLAlchemy internally (it refers to the InstanceState for the instance.
first() applies a limit of one within the generated SQL, so that only one primary entity row is generated on the server side (note this may consist of multiple result rows if join-loaded collections are present). Calling Query. first() results in an execution of the underlying query.
According to the official documentation, these are the available methods SQLAlchemy provides for JSON Fields.
JSON provides several operations:
Index operations:
data_table.c.data['some key']
Index operations returning text (required for text comparison):
data_table.c.data['some key'].astext == 'some value'
Index operations with a built-in CAST call:
data_table.c.data['some key'].cast(Integer) == 5
Path index operations:
data_table.c.data[('key_1', 'key_2', ..., 'key_n')]
Path index operations returning text (required for text comparison):
data_table.c.data[('key_1', 'key_2', ..., 'key_n')].astext == \
'some value'
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSON
From what I know about the JSON type in PostgreSQL, it's best used only if you would want the whole JSON object. If you want to make SQL like operations on fields of the JSON object, then it's best to use classic SQL relations. Here's one source I found saying the same thing, but there are many more. http://blog.2ndquadrant.com/postgresql-anti-patterns-unnecessary-jsonhstore-dynamic-columns/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With