我使用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)))
正确过滤出path
以res_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))))