Django的迁移是什么? [英] What is Django's migration?

查看:145
本文介绍了Django的迁移是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在围绕这个概念工作,当我开始一个新的django项目时,
会促使我应用迁移:

I've been wrapping my head around this concept for a while, when I start a new django project, it urges me to apply migrations:

# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

为什么需要这样做?有人可以给出一个简洁的解释和 migration 的简单用例吗?

why do I need to do it? could anybody give a concise explanation and a simple use case of migration?

编辑:我决定发布此问题因为我没有在stackoverflow上看到任何类似的问题,所以也许有比文档中更好的解释。

I decided to publish this question because I didn't see any similar question here on stackoverflow, maybe there is a better explanation than that in the doc.

推荐答案

迁移是管理数据库架构更改的一种好方法。这有2个主要好处(我认为):

Migrations are a great way of managing database schema changes. This has 2 main benefits (in my opinion):

1-如果您有一个应用程序的多个实例-开发+生产是典型的最低要求,但可能是由其他人安装的应用程序(独立或与其他Django应用程序一起安装),迁移使开发人员可以安全地传播数据库架构更改。控制方式。您可以保证您的应用程序的任何最新版本(即包括最新的数据库模型)都可以正常运行。因此,通常的答案是,迁移以相对优雅的方式解决了一个非常普遍的问题。

1 - If you have multiple instances of an application - development + production being the typical minimum, but could potentially be an application installed by others (either standalone or together with other Django applications), migrations allow you, the developer, to propagate database schema changes in a safe & controlled way. You can guarantee that any up-to-date version of your application (i.e., including your latest database models) will have a functioning database to match. So the general answer is that migrations solve a very common problem in a relatively elegant manner.

2-特别是,正如另一个答案所述,即使是对于与用户和权限有关的绝对全新的Django项目。我能想到的任何有意义的Django应用程序都需要这些表才能运行-即,如果您不使用它们,则可以说,您将无法从Django作为框架提供的许多功能中受益。通过包括迁移,开发人员可以决定要使用的数据库(SQLite,MySQL,PostgreSQL等)以及该数据库的驻留位置,并在进行了这些设置(通常在项目的settings.py中)后,进行迁移照顾其余的人。

2 - Specifically, as noted in another answer, there are initial migrations, even for an absolutely brand-new Django project, relating to users and permissions. Any Django application of any significance that I can think of will need these tables to function - i.e., if you don't make use of them then you are, arguably, not benefiting from much of what Django has to offer as a framework. By including migrations, you the developer can decide what database to use (SQLite, MySQL, PostgreSQL, etc.) and where that database will reside and once you have made those settings (typically in settings.py for the project), the migrations take care of the rest.

这篇关于Django的迁移是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆