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

MySQL strict mode

Open ad-m opened this issue 9 years ago • 6 comments

Hello,

Django 1.10 introduces:

?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.10/ref/databases/#mysql-sql-mode

It looks unsupported in django-environ unfortunately.

Greetings,

ad-m avatar Aug 16 '16 05:08 ad-m

The best place to enable strict mode is in your MySQL server config (whether that be the .cnf file for self-hosted, or via eg the Amazon RDS parameter groups).

Enabling it via the Django DB config is a workaround at best IMO.

edmorley avatar Aug 19 '16 14:08 edmorley

It's worth also noting that strict mode is enabled by default for all of:

  • mysql-server >=5.7.5
  • Amazon RDS's default.mysql5.6 profile
  • mysql-server-5.6 if obtained from mysql.com and not via a distro package

For more details, see: http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_sql_mode http://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sql-mode-strict http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sql_mode http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict

edmorley avatar Aug 26 '16 14:08 edmorley

I think exists environment where is no way to manage MySQL server config eg. service like Heroku, but with MySQL database rather than PostgreSQL . Example from Poland it is mydevil.net, megiteam.pl which support MySQL & Django. This settings has impact on whole SQL server, so it can break other apps on shared SQL server shared by a few small application. In some it need to be configured on connection level, so it's good to add support for that in django-environ.

ad-m avatar Aug 26 '16 18:08 ad-m

I tried to add ?%27init_command%27%3A%20%22SET%20sql_mode%3D%27STRICT_TRANS_TABLES%27%22 To my database_url, but unfortunately it doesn't seem to work :(

gabn88 avatar Nov 26 '19 17:11 gabn88

I solved my problem by using the following configuration

DATABASE_URL=mysql://user:password@host:port/dbname?sql_mode=STRICT_TRANS_TABLES

rodrigoluissilva avatar Jun 08 '23 09:06 rodrigoluissilva

Confirmed to work for my instance in PythonAnywhere. Much cleaner than other options provided.

This appears to be enabled in the db_url_config method of the Env class by the following:

if url.query:
    config_options = {}
    for k, v in parse_qs(url.query).items():
        if k.upper() in cls._DB_BASE_OPTIONS:
            config.update({k.upper(): _cast(v[0])})
        else:
            config_options.update({k: _cast_int(v[0])})
    config['OPTIONS'] = config_options

This really should be documented to make it more discoverable.

thirdjal avatar Jun 08 '23 23:06 thirdjal