egidra
egidra

Reputation: 9087

IntegrityError using Django's loaddata when switching to Postgres

I am getting the following error:

IntegrityError: duplicate key value violates unique constraint "users_userprofile_pkey"

I am migrating from MySQL to Postgres, so I am dumping the data from the MySQL database using:

python2.7 manage.py dumpdata --indent=4 --natural > dump.json

I get the error when I attempt to load the dump.json into the Postgresql database:

 python manage.py loaddata dump.json

I have the following signals in my users/model:

post_save.connect(create_user_profile, sender=User, dispatch_uid="user_create_profile")
post_save.connect(create_api_key, sender=User, dispatch_uid="user_create_api_key")

Upvotes: 1

Views: 2566

Answers (2)

egidra
egidra

Reputation: 9087

I had to comment out the post_save signals and then do the loaddata.

Upvotes: 5

Hassek
Hassek

Reputation: 8995

the problem may be because of the --natural keyword, if you read the docs about dumpdata natural keys here you will see it has some problems that apply to your database migration.

Also here they talk about a solution to this problem which seems to be pretty interesting (and tedious).

You can always try to add first the models that doesn't depend on each other so you can be sure they exists before trying to create another one. i.e:

if you have this models:

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

    birthdate = models.DateField()

    class Meta:
        unique_together = (('first_name', 'last_name'),)

class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.ForeignKey(Person)

Then migrate the Person class first and then the book class.

Also if you can post the error it would be very helpful (since I could be more specific) but I am pretty certain the problem is a primary key issue

Upvotes: 0

Related Questions