2016-10-19 11 views
3

Flask、SQLAlchemy-Utils、Flask-SQLAlchemyを使用してPostgresのマテリアライズドパスビュー(ltree)を問い合わせしようとしています。 SQLAlchemy-Util Docsは、LTreeを使って '=='、 '!='演算子の使用法を示しています。 '〜'演算子はどのように使用できますか?SQLALchemy-Utils:LTreeで '〜'演算子を使用

私はsqlalchemy_utils/ltree.pyでコードを見た:

class comparator_factory(types.Concatenable.Comparator): 
    def ancestor_of(self, other): 
     if isinstance(other, list): 
      return self.op('@>')(expression.cast(other, ARRAY(LtreeType))) 
     else: 
      return self.op('@>')(other) 

    def descendant_of(self, other): 
     if isinstance(other, list): 
      return self.op('<@')(expression.cast(other, ARRAY(LtreeType))) 
     else: 
      return self.op('<@')(other) 

    def lquery(self, other): 
     if isinstance(other, list): 
      return self.op('?')(expression.cast(other, ARRAY(LQUERY))) 
     else: 
      return self.op('~')(other) 

    def ltxtquery(self, other): 
     return self.op('@')(other) 

これはLtreeTypeのサブクラスです。

シンプル==のために、私が使用しています:

Model.query.filter(Model.path == LTree('1.2')).all() 

しかし、この式を使用して、検証エラーがスローされます。

Model.query.filter(Model.path == LTree('~1.2')).all() 

どのように私は、有効なSQLAlchemyのクエリで表現上記のフォーマットができますか?

答えて

3

このコードでこの問題を解決できました。

礼儀のGithub-問題:SQLAlchemy-Util Issues (253)

from sqlalchemy.sql import expression 
from sqlalchemy_utils.types.ltree import LQUERY 

custom_lquery = '*.some.pattern' 
Model.query.filter(Model.path.lquery(expression.cast(custom_lquery, LQUERY))).all() 
関連する問題