Django将模式从sqlite迁移到postgresql

编程入门 行业动态 更新时间:2024-10-23 05:50:23
本文介绍了Django将模式从sqlite迁移到postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在开发一个大型的基于django的项目。在我的笔记本电脑上,我使用SQLite3作为数据库。我为我的模型创建了很多迁移。一切似乎都有效。

然后我想在生产服务器上使用PostgreSQL。我准备了一个空数据库,并试图执行 manage.py syncdb 。突然间,由于没有存在关系,我收到错误。

要执行的操作:同步未迁移的应用程序:西装,消息,人性化,imagekit,staticfiles,crispy_forms,存储,django_extensions,localflavor,注册应用所有迁移:[这里列出我的应用程序],网站,user_auth,会话,auth 同步应用程序而不迁移:创建表... 创建表registration_registrationprofile 运行延迟SQL ... 追溯(最近的最后一次调用):文件/ usr / local / lib / python3.5 / dist-packages / django / db / backends / utils.py,第62行,执行 return self.cursor.execute(sql) psycopg2.ProgrammingError:relationuser_auth_account不存在

试图删除我的sqlite数据库并执行 syncdb 与新的sqlite:它创建架构没有问题。

这里我意识到SQLite不使用外部Ke y关系,并且简单地使用所有引用的 integer 类型。所以它适用于sqlite。很聪明,Django。但是我所有的机型都是相互依赖的。而且我想使用不同的数据库。

现在我有一堆只在SQLite上工作的迁移。

我明白,可以为模型首次创建的每个应用程序逐个运行迁移。但是这将是一个地狱。

是否可以检测依赖关系并以正确的顺序运行这些迁移?

解决方案

Django正确处理迁移中的模型之间的依赖关系。它无法处理,以及它明确地警告在文档中是与移植应用程序相关的未迁移应用程序:

但请注意,未迁移的应用程序不能依赖于已迁移的应用程序,由于没有迁移的本质。这意味着它通常不可能有一个未迁移的应用程序有一个ForeignKey 或ManyToManyField迁移的应用程序;有些情况可能会起作用,但最终会失败。

警告

即使事情似乎与未迁移的应用程序配合使用,这取决于迁移的应用程序,Django可能无法生成所有必需的外键约束!

在这种具体情况下,似乎注册应用程序具有依赖关系在用户模型上。您需要将此应用程序升级到具有必需的迁移文件以支持Django 1.7 +的版本。

I am developing a large django-based project. On my laptop I use SQLite3 as a database. I have created a lot of migrations for my models. Everything seems working.

Then I want to use PostgreSQL on the production server. I have prepared an empty database and trying to do manage.py syncdb. And suddenly I get an error due to not existing relation.

Operations to perform: Synchronize unmigrated apps: suit, messages, humanize, imagekit, staticfiles, crispy_forms, storages, django_extensions, localflavor, registration Apply all migrations: [here list of my apps], sites, user_auth, sessions, auth Synchronizing apps without migrations: Creating tables... Creating table registration_registrationprofile Running deferred SQL... Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 62, in execute return self.cursor.execute(sql) psycopg2.ProgrammingError: relation "user_auth_account" does not exist

Trying to drop my sqlite database and perform syncdb with the new sqlite: it creates the schema without issues.

Here I've realized that SQLite does not use Foreign Key relations and simply uses integer types for all references. So it works for sqlite. Very smart, Django. But all my models are depending of each other. And I want to use different databases.

Now I have a bunch of migrations working on SQLite only.

I understand that it is possible to run migrations one-by-one for each application where the model first gets created. But it would be a hell.

Is it possible to detect dependencies and run these migrations in correct order?

解决方案

Django handles dependencies between models in migrations correctly. What it can't handle, and what it explicitly warns against in the docs, is unmigrated apps with dependencies on migrated apps:

Be aware, however, that unmigrated apps cannot depend on migrated apps, by the very nature of not having migrations. This means that it is not generally possible to have an unmigrated app have a ForeignKey or ManyToManyField to a migrated app; some cases may work, but it will eventually fail.

Warning

Even if things appear to work with unmigrated apps depending on migrated apps, Django may not generate all the necessary foreign key constraints!

In this specific case is seems that the registration app has a dependency on the user model. You need to upgrade this app to a version that has the necessary migration files to support Django 1.7+.

更多推荐

Django将模式从sqlite迁移到postgresql

本文发布于:2023-10-15 01:03:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   Django   postgresql   sqlite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!