2013-04-01 13 views
8

Jak filtrować w sqlalchemy według długości łańcucha?Jak filtrować w `sqlalchemy` przez długość łańcucha?

Ten fragment kodu:

sess.query(db.ArticlesTable).filter(or_(
    and_(db.ArticlesTable.shorttext.length > 0), 
     ... 

dał mi następujący błąd:

File "./aggregate_news.py", line 69, in is_acceptable 
    db.ArticlesTable.shorttext.length > 0), 
    File ".../sqlalchemy/orm/attributes.py", line 211, in __getattr__ 
    key) 
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' 
    object associated with ArticlesTable.shorttext has an attribute 'length' 

Gdzie ArticlesTable jest:

class ArticlesTable(Base): 
    __tablename__ = TABLE_ARTICLES 
    id = Column(Integer, primary_key=True) 
    shorttext = Column(String) 
    ... 

Odpowiedz

12

Musisz użyć func SQL function generator stworzyć LENGTH() funkcji :

from sqlalchemy.sql.expression import func 

sess.query(db.ArticlesTable).filter(or_(
    and_(func.length(db.ArticlesTable.shorttext) > 0), 
Powiązane problemy