pythonsqlalchemypython-elixir

Using sqlalchemy.sql.functions.char_length as a filter condition


I'm using Elixir and sqla 0.6, and I'm trying to query my model:

class Document(Entity): 
    using_options(shortnames=True, order_by='doc_date')
    doc_number = Field(Unicode(20),index=True)

...for Documents having numbers of a given length.

I was thinking about something like this:

Document.query.filter(Document.doc_number.char_lenght()==5).all()

...but apparently, char_length, while present in sqlalchemy.sql.functions, is not working here. How can I make it work within declarative idiom, without resorting to direct queries?


Solution

  • Ok, found an answer:

    from sqlalchemy import func
    Document.query.filter(func.char_length(Document.doc_number)==5).all()