3

The following sort is case-sensitive:

MyModel.select().order_by(MyModel.name)
<class '__main__.MyModel'> SELECT t1."id", t1."name" FROM "mymodel" AS t1 ORDER BY t1."name" []

How do I add a NOCASE to make it case-insensitive?

Or is there another way to make it case-insensitive?

I couldn't find anything about it on the documentation.

stenci
  • 8,290
  • 14
  • 64
  • 104
  • 1
    Would `MyModel.select().order_by(fn.Lower(MyModel.name))` work? That's how it works for SQLAlchemy. – Martijn Pieters May 20 '14 at 14:15
  • For SQLite I see that Peewee also supports [custom collations](http://peewee.readthedocs.org/en/latest/peewee/playhouse.html?highlight=collate#sqlite-ext-api-notes), which should be helpful here. – Martijn Pieters May 20 '14 at 14:23

1 Answers1

6

You can use a SQL function (with the fn helper) to lowercase the model name:

MyModel.select().order_by(fn.Lower(MyModel.name)) 

For SQLite, you should be able to use a custom collation as well:

@db.collation()
def collate_case_insensitive(s1, s2):
    return cmp(s1.lower(), s2.lower())

The @db.collation() decorator provided by the SqliteExtDatabase() subclass lets you register a custom collation function which you can then use to sort by:

MyModel.select().order_by(collate_case_insensitive.collation(MyModel.name))

Of course, SQLite already has a built-in NOCASE collation which should be used here instead, but to use it on must build a SQL() object to include the raw SQL:

MyModel.select().order_by(Clause(MyModel.name, SQL('collate NOCASE')))

This echoes how case insensitive ordering works for SQLAlchemy: SQL alchemy case insensitive sort order

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343