我可以删除迁移目录中的django迁移文件吗 [英] Can I delete the django migration files inside migrations directory

查看:342
本文介绍了我可以删除迁移目录中的django迁移文件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人喜欢Django的MVC理想。但是,当我在1.7版中运行Django迁移时,我在其中进行的每个迁移都存储在migrations目录中。如果我删除这些文件,则在迁移时会引发错误。

I personally like django for its MVC ideals. But while i am running Django migrations in version 1.7 each and every migrations i do in it is stored inside the migrations directory. If i delete those file it is throwing an error while migration.

我已经测试过了。我创建了一个新的Django项目并启动了git repo。我在Django中进行了3-4次迁移,导致
3-4个迁移文件位于migrations目录下。我尝试删除较旧的迁移文件(即第一和第二迁移文件),并尝试运行

I Tested like this. I created a new Django project and initiated a git repo . I ran some 3-4 migrations in Django which resulted in 3-4 migration files under the migrations directory. I tried deleting the very older migration files i.e (1st and 2nd migration files) and tried to run

python manage.py makemigrations

这确实会引起一些错误,例如找不到迁移文件。后来我做了一个git stash来恢复已删除的文件。现在,我尝试再次运行相同的命令,并且运行正常。

which does cause some error like "migration files not found". Later i did a git stash which restored the deleted files. Now i tried to run the same command again and it was working fine.

我的问题是,如果一个人在开发过程中在数据库中运行了大约50次更改,则所有迁移文件都是存储在迁移目录中。

What my question is if a person runs some 50 changes in db during development all the migration files are stored in migrations directory. Is it possible to delete those files and do changes to db again without any interruption?

推荐答案

答案是取决于。是否可以删除这些文件并再次对db进行更改?

The answer is "it depends".

如果您要处理生产数据库或某些由于某种原因而无法定期中断的数据库,那么您绝对希望保留迁移文件已应用于您的数据库。

If you are working against a production DB, or some DB that can't periodically blow away for whatever reason, then you absolutely want to keep around the migration files that you've applied to your DB. They should be checked into source control with the rest of your code.

现在,对于像您这样的情况,丢弃50次迁移的最简单方法就是吹牛删除数据库(需要进行50次迁移),并根据您当前的模型从头开始。在开发过程中逐步开发模型时,通常最好定期进行此操作。

Now, for a situation like yours, the easiest way to discard your 50 migrations would be to just blow away the db (and it's 50 migrations) and start from scratch given your current models. It's oftentimes a good idea to do this periodically as you evolve your models during development.

在销毁数据库时销毁模型也是可以的,因为syncdb将建立一个使用当前模型的空白数据库。然后,它可以选择使用任何初始固定装置填充数据库。从概念上讲,在这一点上不再需要迁移任何内容,因此您无需为旧数据库保留旧的迁移。它们不再相关。

Its ok to blow away your models when you blow away your DB because syncdb will build a blank db using your current models. It'll then optionally populate the db using any initial fixtures. Conceptually, there is no longer anything that you've migrated from at such a point, so you don't need to keep around your old migrations for your old db. They are no longer relevant.

通常,删除已应用到数据库的迁移文件并不好,除非您要么1)完全废除了数据库,要么2)首先恢复迁移。

It's not usually good to delete migration files that have been applied to your DB unless you are either 1) blowing away the DB altogether, or 2) reverting the migrations first.

您可能还想知道,将迁移应用于数据库时,还将这些迁移记录在数据库本身的特殊表中。这就是为什么当您仅删除迁移文件时事情变得一团糟。他们必须与迁移表保持同步

You might also appreciate knowing that when you apply migrations to a db it's also recording those migrations in a special table in the db itself. That's why things go haywire when you just delete the migration files. They have to stay in sync with the migration table

这篇关于我可以删除迁移目录中的django迁移文件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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