19

I have the following table in mysql(5.7.12):

class Story(db.Model):
    sections_ids = Column(JSON, nullable=False, default=[])

sections_ids is basicly a list of integers [1, 2, ...,n]. I need to get all rows where sections_ids contains X. I tried the following:

stories = session.query(Story).filter(
        X in Story.sections_ids
    ).all()

but it throws:

NotImplementedError: Operator 'contains' is not supported on this expression
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Shoham
  • 7,014
  • 8
  • 40
  • 40

2 Answers2

16

Use JSON_CONTAINS(json_doc, val[, path]):

from sqlalchemy import func

# JSON_CONTAINS returns 0 or 1, not found or found. Not sure if MySQL
# likes integer values in WHERE, added == 1 just to be safe
session.query(Story).filter(func.json_contains(Story.section_ids, X) == 1).all()

As you're searching an array at the top level, you do not need to give path. Alternatively beginning from 8.0.17 you can use value MEMBER OF(json_array), but using it in SQLAlchemy is a little less ergonomic in my opinion:

from sqlalchemy import literal

# self_group forces generation of parenthesis that the syntax requires
session.query(Story).filter(literal(X).bool_op('MEMBER OF')(Story.section_ids.self_group())).all()

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
  • You can read about generic functions in SQLA [here](http://docs.sqlalchemy.org/en/latest/core/functions.html). – Ilja Everilä Sep 13 '16 at 12:59
  • Are you sure thats the right syntax? its not working for me... no error is raised... it just not continue to the next line... – Shoham Sep 13 '16 at 13:33
  • Should be str(X)... (in my case X was int and only when casting to string it worked). THANKS! – Shoham Sep 13 '16 at 13:45
  • I want to do the searching at 2nd level, how to specify the path? – Toufeeq Dec 24 '20 at 02:10
  • @toufeeq Pass it as the 3rd argument. – Ilja Everilä Dec 24 '20 at 12:27
  • @IljaEverilä I mean to ask, how to pass the wildcard path $[*].some_field. Since * is not allowed in sqlalchemy query – Toufeeq Dec 28 '20 at 03:37
  • 1
    It is MySQL that does not allow a wildcard in a json path with [`JSON_CONTAINS`](https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-contains). Instead you could search for an object with the required key-value pair: `func.json_contains(Foo.bar, func.json_object("some_field", "some_value"))`. – Ilja Everilä Dec 28 '20 at 08:30
14

For whoever get here, but is using PostgreSQL instead: your fields should be of the type sqlalchemy.dialects.postgresql.JSONB (and not sqlalchemy_utils.JSONType) -

Then you can use the Comparator object that is associated with the field with its contains (and others) operators.

Example:

Query(Mymodel).filter(MyModel.managers.comparator.contains(["user@gmail.com"]))

(note that the contained part must be a JSON fragment, not just a string)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 2
    `MyModel.managers` in this case is the column name ("managers"), not some special keyword. – Dave Jan 08 '21 at 00:38
  • If the contained part must be a JSON fragment, shouldn't the example be `...contains(['"user@gmail.com"'])`? – hert Oct 04 '22 at 15:37