1

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")

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I have a UserProfile that relies on a User model. How do I only dump the User data (that's built in with Django). Also, what should the order be when two models depend on each other?
for the user you can ´./manage.py dumpdata auth´ after that just dump your UserProfile. for models that depend on each other you should use the tedious approach I mentioned. Migrating a database is no easy task. here is another question that may help you out stackoverflow.com/questions/4964615/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.