Skip to content
Advertisement

How do I get Django to run a case-insensitive query against my MySql 5.7 db?

I’m using Django 2.0 and Python 3.7. I have this model and manager for attempting to find my model by a name, case-insentively. The backing database is MySql 5.7.

Unfortunately, when the query actually gets created, it doesn’t appear Django is doing anything to account for case-insensitivity. This is an example query that results …

What changes do I need to make so that Django executes the query in a case-insensitive way against my MySql database?

Edit: Here is the MySql output for “show create table …”

Advertisement

Answer

You have to change the MySQL database/table collation to utf8_unicode_ci or utf_unicode_ci; the ci acronym at the end stands for case insensitive”.

Also check this Q/A utf8_bin vs. utf_unicode_ci.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement