Let's say I have a table named Table
, which contains a JSONB field data
. One of the records (record 1) has the following JSON object stored there:
{
"A": {},
"B": {}
}
Another record (record 2) has the following JSONB object stored:
{
"A": {},
"B": {},
"C": {}
}
I am struggling to define a way to find records by doing an exact filtering on the keys in the data
field. For example, I would like to retrieve the record with only the keys "A" and "B", in the data
field, i.e. record 1.
I have tried the SQLAlchemy's JSONB
type comparator methods such as the contains() method but this would return both records in case of the following Python query:
Session().query(Table).filter(Table.data.contains({"A": {}, "B": {}})).all()
Is it possible to achieve an exact filtering on (a list of) keys with the available SQLAlchemy filters? Any thoughts are welcome. Many thanks!