[Answered ]-Django 3.2+: implementing case-sensitive string comparisons within MySQL tables

1πŸ‘

βœ…

To validate the solution proposed by @rickjames, I’ll document an implementation of the answer here:

in settings.py:

if app_version == 'prod':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS': {
                'read_default_file': '/home/src/my.cnf'
            }
        }
    }
    DB_IS_MYSQL = True
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
    DB_IS_MYSQL = False

MYSQL_SENSITIVE_COLLATION = 'ascii_bin'  
DB_CASE_SENSITIVE_COLLATION  = {'db_collation': MYSQL_SENSITIVE_COLLATION} if DB_IS_MYSQL else {}
# Use: 
# from django.conf import settings
# pieces = models.CharField(max_length=255, default='empty', unique=True, **settings.DB_CASE_SENSITIVE_COLLATION)

in model.py:

name = models.CharField(max_length=50, **settings.DB_CASE_SENSITIVE_COLLATION)
πŸ‘€Skratt

Leave a comment