30 октября 2012 г.

Django loaddata/dumpdata: IntegrityError: Could not load auth.Permission...

Yesterday I was playing with Django to obtain the best way to do automatically backuping and properly update data on production system during continuously deployments even when data model has changed.
As known, Django has it's own way to work with data, using fixtures and manage.py tools: flush, syncdb, loaddata, dumpdata. It was obviously for me, that loading/updating/uploading SQL dumps is a bad practice, while Django has it's own solution. But when I've tried to update local developer's database with data from production system I have got an error.

Production environment:
(env)root@beta:~#python manage.py dumpdata > initial_data.json

Then download this dump to local developer's environment and do:

(env)MacBook-Pro-Vladimir-2:project ignatev$ python manage.py syncdb

Got:

Problem installing fixture '...': Traceback (most recent call last):
  File ".../env/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 196, in handle
    obj.save(using=using)
  File ".../env/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File ".../env/lib/python2.7/site-packages/django/db/models/base.py", line 529, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File ".../env/lib/python2.7/site-packages/django/db/models/query.py", line 557, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File ".../env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 986, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File ".../env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
  File ".../env/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File ".../An/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
  File ".../env/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File ".../env/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: Could not load auth.Permission(pk=31): (1062, "Duplicate entry '...' for key 'content_type_id'")

Read next for solution!



The solution is very easy. You should know, that when you run syncdb Django is filling data for auth module models. So, you need to dump this way:



Production environment:
(env)root@beta:~#python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > initial_data.json

And then:

(env)MacBook-Pro-Vladimir-2:project ignatev$ python manage.py syncdb

will make your developer's database synced with production one.

It's strange, that such details didn't overviewed in official Django documentation. Am I wrong?





Отправить комментарий