Question

Database is MySQL.

I use the django.contrib.auth.

Here is all the info:

manage.py dumpdata auth > my_auth.json --natural --exclude contenttypes

Then I deleted the entire database (drop database my_database), and created a new database (create database new_database)(use ./manage.py syncdb to create all tables).

Then

manage.py loaddata my_auth.json

I Got an error:

IntegrityError: (1062, "Duplicate entry '3-add_author' for key 2")

What's the problem?

Any help will be greatly appreciated. Thank you!

Was it helpful?

Solution

(Updated)

You're right (re your comments below). The problem is indeed with auth.permission.

When you run syncdb, auth.permission is populated automatically with default values for all installed models. Any subsequent runs of syncdb will add new entries for any models that were recently added.

If at a later stage you reset the database and run syncdb again, the values will be repopulated and depending on the order in which installed models are inspected, the associated permissions may be added in a different order giving it different ids from your previous database (if models were installed in stages).

To avoid this problem, you can either leave auth.permission out when dumping your auth data (as you've already pointed out in your comments), or resetting the auth.permission table before loading your data dump.

Also, it is important to use natural keys (--natural) when dumping out your data so that it does not reference related data using its integer id (which may not be the same when loaded in another db). This feature was introduced in Django 1.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top