【问题标题】:Wagtail moving sqlite to postgres databaseWagtail 将 sqlite 移动到 postgres 数据库
【发布时间】:2018-06-16 22:34:40
【问题描述】:

问题

我有一个空的(已迁移的)postgres 数据库,我想将我的 sqlite 数据库中的数据移动到该数据库中。

我的尝试

  • 我用导出

    ./manage.py dumpdata --exclude auth.permission --exclude contenttypes --natural-foreign
    

    然后使用./manage.py loaddata 将数据加载到 postgres 数据库中。

    这里的问题是 wagtail 需要内容类型,我得到一个运行时错误

    FooPage matching query does not exist. 
    

    /wagtail/wagtailcore/models.py:639 
    return content_type.get_object_for_this_type(id=self.id)
    
  • 不要将contenttypesdumpdata 排除在外。现在loaddata 命令失败并显示IntegrityErrorKey ... already exists

  • 我已经尝试在加载数据之前删除所有 ContentType 模型对象,这样它就不会抱怨重复键。虽然这在使用 sqlite db 时有效,但它在 postgres db 上失败,IntegrityError

    Key (id)=(1) is still referenced from table "wagtailcore_page".
    

使用过的版本

  • django 1.11.9
  • 鹡鸰 1.12.3
  • python 3.5

相关

Problems with contenttypes when loading a fixture in Django

【问题讨论】:

  • 尝试将--natural-foreign 用于dumpdata,参见documentation
  • @rafalmp 我也试过了,我已经相应地更新了问题
  • 在 Django 的 dumpdata 和 loaddata 上浪费了太多时间。它很繁琐,几乎没有用。

标签: python django postgresql sqlite wagtail


【解决方案1】:

当我将 Wagtail 网站从 sqlite 移动到 Postgres 时,我管理了一些东西,所以只是添加以供参考。

我使用this website作为基础,但由于外键问题无法立即删除ContentType对象时遇到错误,如下:

django.db.utils.IntegrityError:在表“wagtailcore_page”上插入或更新违反了外键约束“wagtailcore_page_content_type_id_c28424df_fk_django_co”

详细信息:表“django_content_type”中不存在键 (content_type_id)=(1)。

似乎是由于 Wagtail 页面对象,所以先在 Django shell 中执行以下两行。然后能够按照该页面上的其余说明执行 ContentType 删除和导入。

from wagtail.core.models import Page
Page.objects.all().delete()

【讨论】:

  • 在加载我的数据的那一刻,数据库中没有现有的页面,所以这个答案不适用于我的问题。
  • @Joren 抱歉,这不相关..是的,我也不认为会有任何页面数据,因为它是一个新数据库,我运行的只是基本迁移。我认为这些页面可能是 wagtail 管理页面等。
【解决方案2】:

使用pgloader,您可以执行命令指令将数据从 SQLite 加载到 Postgres。

使用 Homebrew 在 OSX 上安装:

brew install pgloader

创建一个脚本文件。比如migrate.script:

load database
     from sqlite:///path/to/db.sqlite3
     into postgresql:///yourpostgresdbname

 with include drop, create tables, create indexes, reset sequences

  set work_mem to '16MB', maintenance_work_mem to '512 MB';

运行它:

pgloader migrate.script

我使用包含最小 Wagtail 项目(一些页面、修订和图像)的 SQLite 数据库进行了测试,它运行良好。

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 2012-08-14
    • 2019-06-23
    • 2018-11-26
    • 1970-01-01
    • 2019-02-26
    • 2016-09-04
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多