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

From MySQL 5.7 onwards and on fresh installs of MySQL 5.6, the default value of the sql_mode option contains STRICT_TRANS_TABLES. That option escalates warnings into errors when data are truncated upon insertion, so Django highly recommends activating a strict mode for MySQL to prevent data loss (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES).

If you need to customize the SQL mode, you can set the sql_mode variable like other MySQL options: either in a config file or with the entry 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" in the OPTIONS part of your database configuration in DATABASES.

机翻如下:

从MySQL 5.7起以及全新安装的MySQL 5.6,sql_mode选项的默认值包含STRICT_TRANS_TABLES。 当数据在插入时被截断时,该选项会将警告升级为错误,因此Django强烈建议为MySQL激活严格模式以防止数据丢失(STRICT_TRANS_TABLES或STRICT_ALL_TABLES)。

如果您需要自定义SQL模式,则可以像其他MySQL选项一样设置sql_mode变量:在配置文件中或在数据库配置的OPTIONS部分中使用条目'init_command':“ SET sql_mode ='STRICT_TRANS_TABLES'”进行设置。 数据库。

配置如下:

 1 DATABASES = {
 2     'default': {
 3         'ENGINE': 'django.db.backends.mysql',
 4         'HOST': 'localhost',
 5         'PORT': 3306,
 6         'NAME': '-------',
 7         'USER': 'root',
 8         'PASSWORD': '-------',
 9         'OPTIONS': {
10                 'charset': 'utf8mb4',
11                 'autocommit': True,
12                 'init_command': 'SET sql_mode="STRICT_TRANS_TABLES"',
13         }
14 
15     },
16 }
原文地址:https://www.cnblogs.com/haiton/p/12202696.html