5

What is the reason for the following error? when i try to filter with:

if MyObject.objects.filter(location = aDictionary['address']):

where location is defined as:

location = models.CharField(max_length=100, blank=True, default='')

I get the following error when aDictionary['address'] contains a string with a non-alphanumeric character (for example Kīhei):

  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaul
terrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1267, "Illegal mix of collations (latin1_sw
edish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
prostock
  • 9,327
  • 19
  • 70
  • 118

2 Answers2

7

Alter the database in MySQL like so:

ALTER TABLE foo CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a new database, remember to create with the right collate settings:

CREATE DATABASE foo CHARACTER SET utf8 COLLATE utf8_general_ci;

More discussion here.

a paid nerd
  • 30,702
  • 30
  • 134
  • 179
  • 1
    So, you recommend dropping the database, including all of its tables and possibly production data, in order to change collation? How about changing that to ALTER statements instead? – Jordan Aug 16 '11 at 20:23
  • I _swear_ that when I encountered this problem all of the docs said I needed to DROP and CREATE. So I researched, and you're right. Post edited. – a paid nerd Aug 17 '11 at 04:48
1

Python is using Unicode strings, and your database is not. Change your database collation to use utf8 and you should be fine.

Jamison Dance
  • 19,896
  • 25
  • 97
  • 99