25

I'm loading a fixture created with dumpdata, and getting the following exception:

Problem installing fixture 'db_dump.json': Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
    obj.save(using=using)
  File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 526, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 491, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 869, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: columns app_label, model are not unique

This is with a sqlite3 backend.

Update: Using natural keys doesn't make a difference here.

What does it mean, and why is it happening?

Marcin
  • 44,601
  • 17
  • 110
  • 191

2 Answers2

50

Apparently one of the traps for the unwary is that one must exclude contenttypes when exporting fixtures. (Thanks to subsume on #django for the information).

To exclude content types use the -e option when running the dumpdata command.

$./manage.py dumpdata -e contenttypes > initial_data.json
Dan Abramov
  • 241,321
  • 75
  • 389
  • 492
Marcin
  • 44,601
  • 17
  • 110
  • 191
  • 1
    Yup, been hit by that one a few times, though only when migrating dev dbs from one engine to another. A general rule for test fixtures is that you should know *exactly* what's in them, so you know what's going on in your tests, so you don't have any surprises. – eternicode Jun 23 '11 at 03:14
  • 7
    Something like this worked for me (just for further reference): ./manage.py dumpdata -e contenttypes > initial_data.json . – Juho Vepsäläinen Mar 09 '12 at 07:42
11
python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > initial_data.json
Furkhat
  • 111
  • 1
  • 2