[Fixed]-Django test database is not created with utf8

29👍

TEST_CHARSET and TEST_COLLATION are renamed to CHARSET and COLLATION and moved to TEST dictionary in Django 1.8:

DATABASES = {
    ...
    'TEST': {
        'CHARSET': 'utf8',
        'COLLATION': 'utf8_general_ci',
    }
}
👤dexity

12👍

in settings add:

DATABASES = {
    'default': {
        ...
        'TEST_CHARSET': "utf8",
        'TEST_COLLATION': "utf8_general_ci",
    }
}

9👍

Please see here: https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DATABASE-TEST

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': <db_name>,
        'USER': <user>,
        'PASSWORD': <password>,
        'HOST': <host>,
        'PORT': <port>,
        'TEST': {
            'NAME': <test_db_name>,
            'CHARSET': 'utf8',
            'COLLATION': 'utf8_general_ci',
        },
    },
}

1👍

I had the same problem and spent hours of figuring it out until noticed that

TEST_CHARSET
TEST_COLLATION

should be a part of the DATABASES, not settings.py.
It’s very easy to mix them up…

https://docs.djangoproject.com/en/dev/ref/settings/#testing

👤Maxim

0👍

As others have pointed out, you need something like this to create the database

DATABASES = {
    ...
    'TEST': {
        'CHARSET': 'utf8mb4',
        'COLLATION': 'utf8mb4_unicode_ci',
    }
}

But you also will want to make sure that you have something like this as well, to make sure that your tests communicate with the db with the appropriate charset.

DATABASES = {
    ...
    'OPTIONS': {
        'charset': 'utf8mb4'
     }
}
👤Nate

-2👍

Leave a comment