how to set mysql collation in django?

328 views
Skip to first unread message

Anton Daneika

unread,
Nov 7, 2007, 6:26:27 PM11/7/07
to django...@googlegroups.com
hello all,

i need something like TEST_DATABASE_COLLATION, but in a normal django code -- not in the tests.

i am running the same script in two environments: as a part of unit test and on it's own and get different results. i suspect the reason for getting different results is django's way of setting collations. my unit test fails when TEST_DATABASE_COLLATION option is set to utf8_unicode_ci, but runs ok if it's utf8_swedish_ci, however i do not know the way to set the collation for the application database. it seems strange that this option is only available for testing purposes. does anyone know how do i set different collation?

the rest of this letter is for the curious ones.
here's the example of the unit test script:

class UnknownErrorTestCase(TestCase):
    fixtures = ['collation_error_db_state.json']
    def testUnknownError(self):
        lang = Language.objects.get(pk='sv')
        movie = Movie.objects.get(pk=10)
        movie.keywords.create(word='män', language=lang)

this test passes if TEST_DATABASE_COLLATION is utf8_swedish_ci and fails if it's utf8_unicode_ci (throws _mysql_exceptions.IntegrityError). the Keyword model has uniqueness constraint: unique_together = (("word", "language"),). the fixture loads a Keyword object whose "word" property is "man". this happens because 'man' == 'män' if utf8_unicode_ci collation is used, so trying to insert a new Keyword throws integrity error. the problem arises when i try to ran these three lines of code outside the unit test script where the collation is not read from the TEST_DATABASE_COLLATION.

mezhaka

unread,
Nov 7, 2007, 6:51:44 PM11/7/07
to Django users
got the answer!
it's alway like that -- the teddy bear effect -- as long as you write
the problem down, the solution comes on it's own. all i had to do is
mysql> drop database videodnevnik;
Query OK, 21 rows affected (0.10 sec)

mysql> create database videodnevnik CHARACTER SET utf8 collate
utf8_swedish_ci;
Query OK, 1 row affected (0.00 sec)

now my script runs as expected.

On 8 нояб, 01:26, "Anton Daneika" <mezh...@gmail.com> wrote:
> hello all,
>
> i need something like TEST_DATABASE_COLLATION, but in a normal django code
> -- not in the tests.
>
> i am running the same script in two environments: as a part of unit test and
> on it's own and get different results. i suspect the reason for getting
> different results is django's way of setting collations. my unit test fails
> when TEST_DATABASE_COLLATION option is set to utf8_unicode_ci, but runs ok
> if it's utf8_swedish_ci, however i do not know the way to set the collation
> for the application database. it seems strange that this option is only
> available for testing purposes. does anyone know how do i set different
> collation?
>
> the rest of this letter is for the curious ones.
> here's the example of the unit test script:
>
> class UnknownErrorTestCase(TestCase):
> fixtures = ['collation_error_db_state.json']
> def testUnknownError(self):
> lang = Language.objects.get(pk='sv')
> movie = Movie.objects.get(pk=10)

> movie.keywords.create(word='m n', language=lang)


>
> this test passes if TEST_DATABASE_COLLATION is utf8_swedish_ci and fails if
> it's utf8_unicode_ci (throws _mysql_exceptions.IntegrityError). the Keyword
> model has uniqueness constraint: unique_together = (("word", "language"),).
> the fixture loads a Keyword object whose "word" property is "man". this

> happens because 'man' == 'm n' if utf8_unicode_ci collation is used, so

Reply all
Reply to author
Forward
0 new messages