What is the correct syntax for finding an HSTORE column with ORM ORA (0.8) SQLAlchemy?

I am using a flask with a sqlachemy bulb extension.

I am trying to search for all records that have an hstore key with a specific value.

Here's how to create a column:

from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy.dialects.postgresql import HSTORE from sqlalchemy.ext.mutable import MutableDict db = SQLAlchemy() class BookDB(db.Model): attributes = db.Column(MutableDict.as_mutable(HSTORE), nullable=False, default={ 'disabled' : '0'}, index=True) 

and here is the query that I run:

 results = BookDB.query.filter_by(attributes={ 'disabled' : '0' }).all() 

This passes without errors, but does not find results.

If I do this:

 results = BookDB.query.filter_by(attributes['disabled']='0').all() 

I get the error: "SyntaxError: keyword cannot be an expression"

If I use filter () instead of filter_by (), I can do

 results = BookDB.query.filter(BookDB.attributes['disabled']=='0').all() 

and it works great and gives the correct results.

But what is the syntax for working with filter_by ()?

+4
source share
1 answer

filter_by is just a convenient shortcut when comparing simple field equality. It accepts only keyword arguments, and therefore only valid Python names are accepted. In this case, it is correct to use filter .

+3
source

Source: https://habr.com/ru/post/1490075/


All Articles