python - django 项目的 migrations 目录是否应该提交到 git

查看:675
本文介绍了python - django 项目的 migrations 目录是否应该提交到 git的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如题,本地开发环境修改 model 时,有些时候会变动好几次,然后就生成了很多 migrations 文件。

但是部署到服务器时,服务器端应该怎么执行变动:

  1. 不上传 migrations 文件,直接执行 makemigrations 重新生成 migrations,再运行 migrate

  2. 上传开发时的 migrations 文件,然后直接执行 migrate

上面两种方法该选哪一种?为什么?

解决方案

按照官方的说法,应该提交,并且在服务器端应该直接执行 migrate,无需再次生成。

You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

The migration files for each app live in a migrations directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleagues’ machines, your staging machines, and eventually your production machines.

中文翻译:

你可以想象 migrations 相当一个你的数据库的一个版本控制系统。makemigrations 命令负责保存你的模型变化到一个迁移文件 - 和 commits 很类似 - 同时 migrate负责将改变提交到数据库。

每个 app 的迁移文件会保存到每个相应 app 的migrations文件夹里面,并且准备如何去执行它, 作为一个分布式代码库。 每当在你的开发机器或是你同事的机器并且最终在你的生产机器上运行同样的迁移,你应当再创建这些文件。

这篇关于python - django 项目的 migrations 目录是否应该提交到 git的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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