网站首页 文章专栏 django 数据迁移loaddata 命令错误 UNIQUE constra int failed
django 数据迁移loaddata 命令错误 UNIQUE constra int failed
编辑时间:Dec. 5, 2017, 1:47 p.m. 作者:赵彦昌 浏览量:7130

今天把网站迁移到hostus VPS 上时,数据迁移用的django 原生工具 python manage.py loaddata mysqlsite.json 报如下错误:

(之前的mysql 现在用的sqlite3)



..........................................


   self.loaddata(fixture_labels)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 100, in loaddata
    self.load_label(fixture_label)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 157, in load_label
    obj.save(using=self.using)
  File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/base.py", line 173, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 762, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 827, in _save_table
    forced_update)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 877, in _do_update
    return filtered._update(values) > 0
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 580, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 1062, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 318, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: Problem installing fixture '/web/mysite_all_data.json': Could not load contenttypes.ContentType(pk=13): UNIQUE constraint failed: django_content_type.app_label, django_content_type.model




google 到简书的一篇文章,可以 试下,在做备份(dumpdata)时,加上以下高亮代码:

python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > mysite_alldata.json


再次导入:

python manage.py loaddata mysqlsite_alldata.json


参考http://www.jianshu.com/p/ef971ac0131f


来说两句吧
最新评论