Django LoadData由于并发数据库查询而失败



我正在尝试将我的django项目的数据库从sqlite迁移到mysql。我首先用./manage.py dumpdata > dump.json倾倒了整个内容,并用./manage.py migrate准备了数据库,并在表中删除了任何创建的数据(这是必要的,因为转储持有 all the Data)。

当我想使用./manage.py loaddata将数据导入新数据库时,我可以解决很多错误,但是我找不到此错误的来源:

Processed 330984 object(s).Traceback (most recent call last):
  File "/app/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 69, in handle
    self.loaddata(fixture_labels)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 109, in loaddata
    self.load_label(fixture_label)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/loaddata.py", line 175, in load_label
    obj.save(using=self.using)
  File "/usr/local/lib/python3.5/site-packages/django/core/serializers/base.py", line 205, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 837, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 904, in _save_table
    forced_update)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 954, in _do_update
    return filtered._update(values) > 0
  File "/usr/local/lib/python3.5/site-packages/django/db/models/query.py", line 664, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1199, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
    raise original_exception
  File "/usr/local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 60, in execute
    self.db.validate_no_broken_transaction()
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 448, in validate_no_broken_transaction
    "An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: Problem installing fixture '/path/to/django/dump.json': Could not load auth.User(pk=1): An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

我已经尝试删除所有信号接收器,因此执行LoadData时我自己的代码都没有运行。

其他人是否在Django的loaddata上经历了类似的行为并设法使其正常工作?

上下文:

  • Python v3.5
  • django v1.11.4
  • 只有使用的stdlib django模型字段

这比解决方案更像是解决方法,但确实解决了我的问题。

进一步调查后,我意识到Auth.user模型以某种方式触发了查询并打破了导入。解决方案是编写一个简单的脚本以将转储分为多个部分(我不再可以访问数据库),然后以正确的顺序导入它们:contenttypesauth和其余。

您可以在这里找到脚本。
这就是我将转储分开的方式:

./investigate-django-dump.py dump.json extract contenttypes. > contenttypes.json
./investigate-django-dump.py dump.json extract auth. > auth.json
./investigate-django-dump.py dump.json extractnot auth.,contenttypes. > data.json

最新更新