pythonmysqlsqlalchemysoundex

How to use MySQL SOUNDEX function with SQLAlchemy


Looking for any example of making SOUNDEX queries on MySQL from SQLAlchemy, if possible at all. Any alternatives?


Solution

  • If all you need is to use the SOUNDEX() function, then just use func to generate the function expression:

    session.query(func.soundex(MyModel.some_str))
    

    If on the other hand you need the SOUNDS LIKE operator, you can use op():

    session.query(MyModel).\
        filter(MyModel.some_str.op('SOUNDS LIKE')('Supercalifragilisticexpialidocious'))
    

    which is equivalent to

    session.query(MyModel).\
        filter(func.soundex(MyModel.some_str) ==
               func.soundex('Supercalifragilisticexpialidocious'))