简体   繁体   中英

Sqlalchemy search JSONB column with arrays and no keys

I'm trying to filter a JSONB column in flask-sqlalchemy and am having trouble:

class Journal(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    synonyms = db.Column(JSONB)

j = Journal(synonyms=['red', 'blue'])
db.session.add(j)
db.session.commit()

records = Journal.query.filter(Journal.synonyms.contains('red')).all()

I get the error: sqlalchemy.exc.InternalError: (psycopg2.errors.InFailedSqlTransaction) current transaction is aborted, commands ignored until end of transaction block

Any idea what I am doing wrong?

I figured out the correct query for this. It is:

import json
records = Journal.query.filter(Journal.synonyms.contains(json.dumps('red'))).all()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM