我试图查询Postgres物化路径视图(Ltree),使用Flask、SQLAlchemy和Flask。SQLAlchemy文档显示了使用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查询中格式化上述表达式?
发布于 2016-12-15 07:45:02
我用这段代码解决了这个问题。
礼节-问题:SQLAlchemy-Util问题(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()https://stackoverflow.com/questions/40137655
复制相似问题