1

In a Python 2 Django app, I'm defining a string with a 'money with wings' emoji:

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

This passes the pylint linter fine.

However, I also have a model Message with a content field, and if I try to write to there, I get an error message:

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

Below is the full stack trace. How can I fix this? Since utf8mb4_general_ci is MySQL-specific, I suppose that the encoding the database is currently in, and utf8_general_ci is the encoding that I'm defining my string to be. How can I define my string to be in utf8mb4_general_ci instead of 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 '='")
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • FYI, I believe it's the emoji that's in `utf8mb4_general_ci`. is 4 bytes. – cody Dec 07 '18 at 23:52
  • What's the collation of your database? And what's the collation of the column that you are writing to, if it isn't the same as the db? – snakecharmerb Dec 08 '18 at 11:44
  • The problem occur on your `CHARSET` and `COLLATE`. `ALTER TABLE Tab CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci`. – KC. Dec 08 '18 at 12:00
  • Please show us the generated SQL statement that is involved. – Rick James Dec 08 '18 at 21:25
  • `CHARACTER SET utf8mb4` is for encoding, which is relevent to the question. `COLLATION` is for ordering, which is not relevant. – Rick James Dec 08 '18 at 21:26

0 Answers0