I have an existing SQLAlchemy model that is supposed to have a relation to another model, however the connecting point there is sadly a (sub)field of a JSON PostgreSQL column.
So let's say one table "category" is:
id(bigint) | name(string)
-------------------------
5 | Comics
... and second table "hero" is:
id(bigint) | name(string) | info(JSON)
-------------------------------------------------------------
2 | Tranquility | {"category_id":5, "genre_id": 17}
How would I, in python SQLAlchemy&PostgreSQL9.5, join category
on the hero
through the hero
's info["category_id"]
to get the following results?
id(bigint) | name(string) | category_name | info(JSON)
-------------------------------------------------------------
2 | Tranquility | Comics | {"category_id":5, "genre_id": 17}
I know such things are possible in the PostgreSQL itself according to this answer
https://dba.stackexchange.com/a/83935 ( http://sqlfiddle.com/#!15/226c33/1 )
And that there probably needs to be some trickstery of casting or bypassing default ways
as described in sqlalchemy filter by json field
Note: Please note that I can't change the DB structure nor the existing parts of the model and I am also not looking for a double query bypass which I already know.