Incorrect String Value Error when Saving Unicode Strings in MySQL
When attempting to save unicode strings to Django's auth_user model, an "incorrect string value" error may occur. This is because MySQL has a 3-byte limit on utf-8 characters, while some unicode characters require 4 bytes.
Root Cause:
The error occurs because MySQL cannot store certain unicode characters due to its byte limitations.
Solution:
To resolve this issue:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', ... 'OPTIONS': {'charset': 'utf8mb4'}, } }
Alternatives:
It's important to note that by switching to utf8mb4, you'll need to be aware of potential compatibility issues with older versions of MySQL or other applications that may not support the 4-byte limit.
The above is the detailed content of How to Fix \'Incorrect String Value\' Errors When Saving Unicode Strings in MySQL with Django?. For more information, please follow other related articles on the PHP Chinese website!