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")
egidra
  • 8,537
  • 19
  • 62
  • 89

2 Answers2

5

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

egidra
  • 8,537
  • 19
  • 62
  • 89
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

Hassek
  • 8,715
  • 6
  • 47
  • 59
  • 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? – egidra Aug 09 '12 at 22:57
  • 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 http://stackoverflow.com/questions/4964615/how-can-i-easily-convert-a-django-app-from-mysql-to-postgresql – Hassek Aug 09 '12 at 23:12