Bhargav Patel
Bhargav Patel

Reputation: 151

Can not query the json column with sqlachemy

I am trying to query with in json column in postgresql with flask-sqlalchemy. here is my code

house_ = House()
results = house_.query.filter(
            House.information['branch_name'].astext == 'release0'
            ).all()

I am not sure what is wrong. I tried to use .cast(Unicode) instead of astext as well.

Getting error as below:

NotImplementedError: Operator 'getitem' is not supported on this expression

Upvotes: 1

Views: 1519

Answers (1)

antonio_antuan
antonio_antuan

Reputation: 1313

You shold use 'op' method in your query, like this:

Session.query(Model).filter(Model.json_field.op('->')('KEY') == VALUE) 

Also, you can use - >> JSON operator for auto-cast value to text. Also, you can read more about Postgresql JSON(B) operators :)

Upvotes: 6

Related Questions