Wagtail将SQLite迁移到PostgreSQL数据库

7

问题

我有一个空的(迁移的)Postgres数据库,我想将数据从我的SQLite数据库移动到其中。

我已经尝试过的方法

  • I export with

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

    and consequtively load the data into the postgres db with ./manage.py loaddata.

    The problem here is that wagtail requires the contenttypes and I get a runtime error

    FooPage matching query does not exist. 
    

    at

    /wagtail/wagtailcore/models.py:639 
    return content_type.get_object_for_this_type(id=self.id)
    
  • Don't exclude contenttypes with dumpdata. Now the loaddata command fails with an IntegrityError: Key ... already exists.

  • I have tried to remove all ContentType model objects before loading in the data so that it doesn't whine about duplicate keys. While that works when using the sqlite db, it fails on the postgres db with an IntegrityError

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

使用版本

  • django 1.11.9
  • wagtail 1.12.3
  • python 3.5

相关内容

在Django中加载fixture时出现contenttypes问题的解决方法


尝试使用--natural-foreign命令进行dumpdata,请参阅文档 - rafalmp
@rafalmp 我也尝试过那个方法,我已经相应地更新了问题。 - Joren
在Django的dumpdata和loaddata上浪费了太多时间。它很麻烦,而且几乎从来没有成功过。 - allcaps
2个回答

2

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

在OSX上使用Homebrew进行安装:

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数据库进行了测试,并且它完美地工作了。


这个解决方案在Ubuntu / Debian上运行得非常顺畅。非常好的解决方案。 - sequence

0

当我将我的Wagtail网站从sqlite迁移到Postgres时,我做了一些管理工作,所以只是为了参考而添加。

我使用这个网站作为基础,但遇到了一个错误,因为由于外键问题,我无法立即删除ContentType对象,如下所示:

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

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

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

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

在加载我的数据时,数据库中不存在任何现有页面,因此此答案不适用于我的问题。 - Joren
@Joren 对不起,它与主题无关。是的,我也没有想到会有任何页面数据,因为它是一个全新的数据库,而且我只运行了基本的迁移。我认为这些页面可能是 wagtail 管理页面等。 - Ian Cameron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接