如何用lquery过滤掉?



我使用LQUERY在我的PostgreSQL -数据库查询中做一些自定义过滤。其中一个字段是ltree字段,称为path,我应该能够检查哪些对象在其path中具有特定字符串并将其过滤掉。

我试过了:

from sqlalchemy_utils.types.ltree import LQUERY
from sqlalchemy.sql import expression
custom_lquery = '!res_1.*'
stmnt = stmnt.filter(MyModel.path.lquery(expression.cast(custom_lquery, LQUERY)))

正确过滤出pathres_1开头的对象。我想做的是过滤掉所有在path的任何点上都有res_1的对象,例如res_0.res_1.res_2。我怎样才能做到这一点呢?

这是通过SQLAlchemynot_实现的。

from sqlalchemy import not_
custom_lquery = '*.res_1.*'
stmnt = stmnt.filter(not_(MyModel.path.lquery(expression.cast(custom_lquery, LQUERY))))

最新更新