mac下在django中执行manage.py migrate命令后出现django.db.utils.OperationalError: (1366, "Incorrect string value

在django中执行manage.py migrate命令后出现如下报错

django.db.utils.OperationalError: (1366, "Incorrect string value

首先要保证代码头添加了

# -*- coding: utf-8 -*-
并且字符串前要有加unicode标识如 u'你好'

如果还是报错,可以去数据库排查一下编码问题

进入mysql命令行

mysql> show variables like '%char%';  
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | latin1                                                    |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | latin1                                                    |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.21-macos10.13-x86_64/share/charsets/ |

如上,其中的编码有latin1的所以需要修改latin1为utf8,以下是mac中的修改办法

vim /etc/mysql/my.cnf

添加如下内容

[client] 

default-character-set =utf8

[mysql] 

default-character-set =utf8

[mysqld]

collation-server = utf8_unicode_ci
init_connect ='SET NAMES utf8'
character_set_server=utf8

然后重启mysql服务即可

新版的mysql貌似不会出现该问题,但没有亲自尝试。

你可能感兴趣的:(数据库)