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

4πŸ‘

βœ…

(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.

πŸ‘€Shawn Chin

Leave a comment