4

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?

Ben
  • 51,770
  • 36
  • 127
  • 149
AlexVhr
  • 2,014
  • 1
  • 20
  • 30
  • just a suggestioin: post your answer below, and accept it as the answer so people immediately will know that your problem is solved – kusut Jul 08 '11 at 11:41
  • @kusut I was trying to do what you suggested, but code markup didn't seem to work in the comment editor, and I got confused by the interface... I'll try again, thanks. – AlexVhr Jul 08 '11 at 12:08

1 Answers1

6

Ok, found an answer:

from sqlalchemy import func
Document.query.filter(func.char_length(Document.doc_number)==5).all()
AlexVhr
  • 2,014
  • 1
  • 20
  • 30
  • 1
    I guess it's moved in a more recent version but it's now in `sqlalchemy.sql.functions.char_length` – GP89 Aug 20 '13 at 15:32