[Django] dumpdata to avoid conflicts and errors at loaddata

Technical Jul 13, 2017

When you backup whole database by using dumpdata command, it will backup all the database tables.

If you use this database dump to load the fresh database(in another django project), it can be causes IntegrityError (If you loaddata in same database it works fine).

To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables.

./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

Long version, with natural primary and foreign keys:

manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 4

Tags

Florin Tomozei

Software Engineer and Storyteller