在具有MySQL后端的Django上,“操作'='的排序规则(utf8_general_ci,IMPLICIT)和(utf8mb4_general_ci,COERCIBLE)的非法混合”

时间:2018-12-07 23:21:10

标签: python mysql django utf-8 character-encoding

在Python 2 Django应用中,我定义了一个带有'money with wings'表情符号的字符串:

# -*- coding: UTF-8 -*-
MESSAGE = "Stash some cash "

这将使pylint棉短罚款通过。

但是,我也有一个带有Message字段的模型content,如果我尝试在其中写入内容,则会收到错误消息:

Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='

下面是完整的堆栈跟踪。我怎样才能解决这个问题?由于utf8mb4_general_ci是特定于MySQL的,因此我假设数据库当前处于编码状态,而utf8_general_ci是我正在定义字符串的编码。如何定义我的字符串在utf8mb4_general_ci中而不是utf8_general_ci中?

/app/venmo_platform/current/venmo/communication/services/notification_service.pyc in get_message_for_notification(message_content)
     99 
    100 def get_message_for_notification(message_content):
--> 101     message, _ = Message.objects.get_or_create(content=message_content)
    102     return message

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
    125         def create_method(name, method):
    126             def manager_method(self, *args, **kwargs):
--> 127                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    128             manager_method.__name__ = method.__name__
    129             manager_method.__doc__ = method.__doc__

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/query.pyc in get_or_create(self, defaults, **kwargs)
    403         self._for_write = True
    404         try:
--> 405             return self.get(**lookup), False
    406         except self.model.DoesNotExist:
    407             return self._create_object_from_params(lookup, params)

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    326         if self.query.can_filter():
    327             clone = clone.order_by()
--> 328         num = len(clone)
    329         if num == 1:
    330             return clone._result_cache[0]

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/query.pyc in __len__(self)
    142 
    143     def __len__(self):
--> 144         self._fetch_all()
    145         return len(self._result_cache)
    146 

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/query.pyc in _fetch_all(self)
    963     def _fetch_all(self):
    964         if self._result_cache is None:
--> 965             self._result_cache = list(self.iterator())
    966         if self._prefetch_related_lookups and not self._prefetch_done:
    967             self._prefetch_related_objects()

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/query.pyc in iterator(self)
    236         # Execute the query. This will also fill compiler.select, klass_info,
    237         # and annotations.
--> 238         results = compiler.execute_sql()
    239         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
    240                                                   compiler.annotation_col_map)

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    838         cursor = self.connection.cursor()
    839         try:
--> 840             cursor.execute(sql, params)
    841         except Exception:
    842             cursor.close()

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/backends/utils.pyc in execute(self, sql, params)
     77         start = time()
     78         try:
---> 79             return super(CursorDebugWrapper, self).execute(sql, params)
     80         finally:
     81             stop = time()

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/backends/utils.pyc in execute(self, sql, params)
     62                 return self.cursor.execute(sql)
     63             else:
---> 64                 return self.cursor.execute(sql, params)
     65 
     66     def executemany(self, sql, param_list):

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
     96                 if dj_exc_type not in (DataError, IntegrityError):
     97                     self.wrapper.errors_occurred = True
---> 98                 six.reraise(dj_exc_type, dj_exc_value, traceback)
     99 
    100     def __call__(self, func):

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/backends/utils.pyc in execute(self, sql, params)
     62                 return self.cursor.execute(sql)
     63             else:
---> 64                 return self.cursor.execute(sql, params)
     65 
     66     def executemany(self, sql, param_list):

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/django/db/backends/mysql/base.pyc in execute(self, query, args)
    122         try:
    123             # args is None means no string interpolation
--> 124             return self.cursor.execute(query, args)
    125         except Database.OperationalError as e:
    126             # Map some error codes to IntegrityError, since they seem to be

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/MySQLdb/cursors.pyc in execute(self, query, args)
    248         except Exception:
    249             exc, value = sys.exc_info()[:2]
--> 250             self.errorhandler(self, exc, value)
    251         self._executed = query
    252         if not self._defer_warnings:

/app/shared/virtualenvs/venmo_platform27/local/lib/python2.7/site-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
     48     del connection
     49     if isinstance(errorvalue, BaseException):
---> 50         raise errorvalue
     51     if errorclass is not None:
     52         raise errorclass(errorvalue)

OperationalError: (1267, "Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='")

0 个答案:

没有答案