开发者

startswith on a constant in sqlalchemy

I'm trying to do something like this:

Session.query(some_table).filter("/some/path/to/element".startswith(some_table.path)).all()

That is, get all "parent" elements of a certain path. I tried doing it like this:

Session.query(some_table).filter(sqlalchemy.sql.expression.literal("/some/path/to/element").startswith(some_table.pat开发者_StackOverflow中文版h)).all()

But I get some weird exceptions with it. I wonder if what I want is actually even possible.


i don't understand what you are try to do, because it not that filter just get a boolean filter should get an expression that will loop over it to filter data.

look at this example (i don't think you want this one but maybe it can give you some Hint):

Session.query(some_table).filter(some_table.path.like('%/some/path/to/element'))

or this example because if i understand well you want to get all parent path of a given path:

import os

a = '/some/path/to/element'
parent_path = []
# Get all parent path
while a != '/':
    a = os.path.split(a)[0]
    parent_path.append(a)

Session.query(some_table).filter(some_table.path.in_(parent_path))

Hope this will help


Actually the second example that I gave works, I just made a silly mistake in the column name. I'm sorry for wasting your time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜