Sajad Norouzi
Sajad Norouzi

Reputation: 1920

migrating to mysql in django

I want to migrate from sqlite3 to MySQL in Django. First I used below command:

python manage.py dumpdata > datadump.json

then I changed the settings of my Django application and configured it with my new MySQL database. Finally, I used the following command:

python manage.py loaddata datadump.json

but I got this error :

integrityError: Problem installing fixtures: The row in table 'django_admin_log' with primary key '20' has an invalid foregin key: django_admin_log.user_id contains a value '19' that does not have a corresponding value in auth_user.id.

Upvotes: 2

Views: 4153

Answers (2)

Micha Fellner
Micha Fellner

Reputation: 1

I had to move my database from a postgres to a MySql-Database.

This worked for me:

Export (old machine):

python manage.py dumpdata --natural --all --indent=2 --exclude=sessions  --format=xml > dump.xml

Import (new machine): (note that for older versions of Django you'll need syncdb instead of migrate)

manage.py migrate --no-initial-data

Get SQL for resetting Database:

manage.py sqlflush

setting.py:

DATABASES = {
'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'asdf',
        'USER': 'asdf',
        'PASSWORD': 'asdf',
        'HOST': 'localhost',
    #IMPORTANT!!
    'OPTIONS': {
             "init_command": "SET foreign_key_checks = 0;",
        },
}

python manage.py loaddata dump.xml

Upvotes: 0

Ilia Novoselov
Ilia Novoselov

Reputation: 363

You have consistency error in your data, django_admin_log table refers to auth_user which does not exist. sqlite does not enforce foreign key constraints, but mysql does. You need to fix data and then you can import it into mysql.

Upvotes: 1

Related Questions