If you transfer the path variable to a bindparam() object, you can treat it like any column, including using .contains() and .startswith() :
from sqlalchemy.sql.expression import bindparam session.query(Site).filter(bindparam('path', path).contains(Site.path_prefix))
SQLAlchemy translates .contains() to:
? LIKE CONCAT('%', Site.path_prefix, '%')
in MySQL or
? LIKE '%' || Site.path_prefix || '%'
in other databases.
If you want to check the work of .startswith() , this also works:
from sqlalchemy.sql.expression import bindparam session.query(Site).filter(bindparam('path', path).startswith(Site.path_prefix))
Martijn pieters
source share