Regular expressions in SQLalchemy queries?

前端 未结 3 1502
旧时难觅i
旧时难觅i 2020-12-29 20:56

Is it possible to use a regex in a way similar to session.query(MyObject).filter_by(REGEX)?

If not, how can I use sqlAlchemy to retrieve records that ha

3条回答
  •  梦毁少年i
    2020-12-29 21:37

    [DISCLAIMER: no regex]

    I'm answering to the question "how can I use sqlAlchemy to retrieve records that have a varchar PK beginning with a certain value" because for this simple use case a LIKE probably is both less expensive and more portable (asking for regular expressions seems like a manifestation of the XY Problem).

    In SQLAlquemy (borrowing from Alex):

    session.query(Object).filter(Object.column.like('something%'))
    

    In SqlSoup I use:

    db.table.filter(db.table.column.like('something%')) 
    

提交回复
热议问题