G
G
greenpower2015-07-07 22:48:39
Python
greenpower, 2015-07-07 22:48:39

How to make Cyrillic case-insensitive search work in SQLAlchemy/Postgres?

I am using SQLAlchemy and PostgreSQL DBMS.
Let there be:

class Product(db.Model):
  __tablename__ = 'products'
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String)

  def __init__(self, name):
    self.name = name

and done
>>> p1 = models.Product(u'Курага')
>>> db.session.add(p1)
>>> db.session.commit()
>>> p2 = models.Product(u'Apple')
>>> db.session.add(p2)
>>> db.session.commit()
>>> from sqlalchemy import func
>>> db.session.query(models.Product).filter(func.lower(models.Product.name) == u'курага').all()
[]
>>> db.session.query(models.Product).filter(func.lower(models.Product.name) == u'apple').all()
[<app.models.Product object at 0x1035f4310>]

as well as
>>> db.session.query(models.Product).filter(models.Product.name.ilike(u'%курага%')).all()
[]
>>> db.session.query(models.Product).filter(models.Product.name.ilike(u'%apple%')).all()
[<app.models.Product object at 0x103b2c210>]

How to make it work with Cyrillic?
Thanks in advance and sorry in advance if I didn't google it (:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
greenpower, 2015-07-08
@greenpower

Helped:

psql (9.4.1)
Type "help" for help.

postgres=# CREATE DATABASE dbname ENCODING 'UTF-8' LC_COLLATE 'ru_RU.UTF-8' LC_CTYPE 'ru_RU.UTF-8' TEMPLATE=template0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question