[英]How do I create a Django migration to set charset and collation for all existing tables and all future ones?
I'm using Django, Python 3.7 and MySql 5.7.我正在使用 Django、Python 3.7 和 MySql 5.7。 I want to set the default charset and collation for all existing and all future tables to
我想将所有现有表和所有未来表的默认字符集和排序规则设置为
DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I have already created some migrations,我已经创建了一些迁移,
(venv) localhost:maps davea$ ls web/maps/migrations/
0001_initial.py __init__.py __pycache__
Is there any way I can create such a migration?有什么办法可以创建这样的迁移吗? I'm happy to blow away from database and start from scratch if that's what it takes.
如果需要的话,我很高兴摆脱数据库并从头开始。
你必须改变数据库的CHARSET,django 与它无关: How to convert a entire MySQL database characterset and collation to UTF-8?
I came across this problem now where I tried creating a new record where the value of a field_x
of type django.db.models.TextField()
contains emoticons.我现在遇到了这个问题,我尝试创建一个新记录,其中
django.db.models.TextField()
类型的field_x
的值包含表情符号。 It resulted to error:结果报错:
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x99\\x82 :...' for column `db_name`.`table_name`.`field_x` at row 1")
I set the charset to utf8mb4 in the proj/settings.py
specifically under the DATABASES
field:我在
proj/settings.py
专门在DATABASES
字段下将字符集设置为 utf8mb4:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "any",
"USER": "any",
"PASSWORD": "any",
"HOST": "any",
"PORT": "any",
"OPTIONS": {
"charset": "utf8mb4",
},
}
}
The creation of the record became successful.唱片的创作取得了成功。 Also, fetching the created record shows that it correctly contains the saved emoticons.
此外,获取创建的记录表明它正确包含保存的表情符号。
>>> record = TableName.objects.get(id=93)
>>> record.field_x
'masters trickeds us!!! MYYYYY PRECIOOOOOUS~! :) 🙂 :) ^_^🙂^__^'
Some further reference you could check:您可以检查一些进一步的参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.