Django:将数据从 SQLite 移动到 PostgreSQL

Posted

技术标签:

【中文标题】Django:将数据从 SQLite 移动到 PostgreSQL【英文标题】:Django: Moving data from SQLite to PostgreSQL 【发布时间】:2018-05-23 00:37:27 【问题描述】:

我最近继承了一个使用 Django 编写的 webapp 的生产代码库。到目前为止,项目使用的数据库一直是默认的 SQLite3 数据库,但现在使用该应用程序的人越来越多,迁移到 Postgres 是必要的。

我已经能够使用运行良好的项目设置一个空的 postgres 数据库。我遇到的问题是将数据从旧项目移动到新项目。我可以跑 python manage.py dumpdata --natural-foreign > dump.json 转储数据,效果很好,但是当我在 settings.py 中切换到 postgres 并运行时 python manage.py loaddata dump.json 我收到以下错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
    self.loaddata(fixture_labels)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
    self.load_label(fixture_label)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 147, in load_label
    obj.save(using=self.using)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 803, in _save_table
    forced_update)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/base.py", line 853, in _do_update
    return filtered._update(values) > 0
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/query.py", line 580, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1062, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/mnt/d/Code/MCJobTrack/_VENV/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: Problem installing fixture '/mnt/d/Code/MCJobTrack/jobtrack_project/dump.json': Could not load contenttypes.ContentType(pk=15): duplicate key value violates unique constraint "django_content_type_app_label_45f3b1d93ec8c61c_uniq"
DETAIL:  Key (app_label, model)=(jobtrack, purchaseorder) already exists.

到目前为止我已经尝试过:

在 dbshel​​l 中运行 TRUNCATE django_content_type RESTART IDENTITY CASCADE; 转储数据时排除内容类型。 (我收到错误 django.core.serializers.base.DeserializationError: Problem installing fixture '/mnt/d/Code/MCJobTrack/jobtrack_project/dump_no_contenttypes.json': ContentType matching query does not exist.) Resetting the primary key sequence 转储数据时使用 --natural-primary 标签

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

您可能还需要使用--natural-primary,以便不将内容类型的主键导出到夹具:

python manage.py dumpdata --natural-foreign --natural-primary > dump.json

【讨论】:

试过了:运行 loaddata 现在会产生错误jobtrack.models.DoesNotExist: Problem installing fixture '/mnt/d/Code/MCJobTrack/jobtrack_project/dump.json': Employee matching query does not exist.【参考方案2】:

原来我需要做两件事:

    使用 --natural-primary、--natural-foreign 和 -e 转储数据 内容类型 Have loaddata ignore post_save signals

加载数据仍然需要很长时间,但它可以工作。

【讨论】:

以上是关于Django:将数据从 SQLite 移动到 PostgreSQL的主要内容,如果未能解决你的问题,请参考以下文章

加载数据转储时,将 Django 项目从 sqlite3 后端切换到 postgresql 失败

Django:将项目从 sqlite 迁移到 PostgreSQL 的最佳实践是啥

South - 将 django 应用程序从 sqlite 迁移到 mysql

将 Django 开发数据库从默认 SQLite 更改为 PostgreSQL

将 Django DB 从 SQLite 迁移到 MySQL 的最佳方法是啥?

将 Django 数据库:SQLITE3 更改为 MARIADB