Sqlalchemy json column - how to preform a contains query

后端 未结 2 835
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 01:38

I have the following table in mysql(5.7.12):

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

section

2条回答
  •  心在旅途
    2021-01-05 02:25

    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)

提交回复
热议问题