django - MySQL strict mode with database url in settings

家住魔仙堡 提交于 2019-12-25 08:50:11

问题


I'm using a database URL string in my settings like:

DATABASES = {
    'default': "mysql://root:@localhost:3306/mydb"
}

When I migrate I get this warning:

MySQL Strict Mode is not set for database connection 'default'

Now my question: How can I combine the two things?

I cannot use the "regular" way to set the database settings with a dictionary because my database url comes from an environment variable.

Thx in advance!


回答1:


You could update your settings afterwards:

DATABASES['default']['OPTIONS'] = {'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"}



回答2:


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'root',
        'PASSWORD': 'test123',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'charset': 'utf8mb4',
        }
    }
}

Try to give init_command of database options in settings.py If want know more refer the docs django_mysql.W001: Strict Mode




回答3:


I think this one will help you. you can pass the options as a query string in the URL

DATABASES = {
    'default': dj_database_url.config(default="mssql://USER:PASSWORD@HOST:PORT/NAME?init_command=SET sql_mode='STRICT_TRANS_TABLES'&charset=utf8mb4", conn_max_age=500)
}


来源:https://stackoverflow.com/questions/46441487/django-mysql-strict-mode-with-database-url-in-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!