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 }