gpt4 book ai didi

flask - SQLALchemy-Utils : Use '~' operator with LTree

转载 作者:行者123 更新时间:2023-12-03 00:56:05 25 4
gpt4 key购买 nike

我正在尝试使用 Flask SQLAlchemy-Utils 查询 Postgres 物化路径 View (ltree)和 Flask-SQLAlchemy。 SQLAlchemy-Util Docs显示使用 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 查询中格式化上述表达式?

最佳答案

我能够用这段代码解决这个问题。

由 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()

关于flask - SQLALchemy-Utils : Use '~' operator with LTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com