开发者

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

开发者 https://www.devze.com 2023-04-04 00:16 出处:网络
Database is MySQL. I use the django.contrib.auth. Here is all the info: manage.py dumpdata auth > my_auth.json --natural --exclude contenttypes

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 th开发者_如何学编程e 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消