django-environ icon indicating copy to clipboard operation
django-environ copied to clipboard

Collation and charset?

Open AndreaCrotti opened this issue 7 years ago • 2 comments

Is there a way to define the DB url in such a way that also the COLLATION and CHARSET would get set as below?

        'mysql': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'django2',
            'USER': 'user',
            'PASSWORD': 'password',
            'HOST': 'localhost',
            'PORT': '3306',
            'OPTIONS': {'init_command': 'SET storage_engine=INNODB'},
            'STORAGE_ENGINE': 'INNODB',
            'COLLATION': 'utf8-general_ci',
            'CHARSET': 'utf-8',
        },

thanks a lot!

AndreaCrotti avatar Oct 19 '16 13:10 AndreaCrotti

Same question here (for an external project). Looking at the source of db_url_config, it's not yet possible but it can be fixed. The STORAGE_ENGINE, COLLATION and CHARSET should be added to _DB_BASE_OPTIONS we we can write:

mysql://user:pass@localhost/django2?COLLATION=utf8-general_ci&CHARSET=utf-8

Adding &STORAGE_ENGINE=INNODB&init_comment=SET storage_engine=INNODB are likely not needed, since these are defaults since MySQL 5.5.5 and that's the minimal version Django supports.

vdboor avatar Dec 20 '16 12:12 vdboor

Got 'TypeError: keywords must be strings.' when doing this.

Update: The correct way is using lower case: '?charset=utf8'

wolfg1969 avatar Mar 02 '17 04:03 wolfg1969