You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目部署疑问:是否需迁移migrations文件夹及执行数据库迁移?

Django Migrations in Production: What’s the Right Approach?

Great question—this is such a common point of confusion when moving Django apps from local development to production! Let’s break down your two questions clearly:

1. Should I deploy the migrations folder to my production server?

Absolutely yes! Migrations files are Django’s "history log" of how your database schema has changed over time. Your production server needs these files to know exactly how to bring its database into alignment with your app’s current model structure. Without them, the migrate command won’t have a reference point to work from—this can lead to missing schema changes, duplicate migration entries, or even broken database state that’s hard to fix.

2. Should I exclude migrations, then run makemigrations and migrate on the server?

Strongly recommend against this! The golden rule here is that your migrations must be identical across development and production environments. If you generate migrations locally, then skip pushing them and re-run makemigrations on the server, you risk creating conflicting migration files (due to subtle environment differences like Django version variations, un-synced model tweaks, etc.). This will cause migration errors, schema mismatches, and potentially corrupt your production database.

The Correct Workflow

  • When you make model changes locally, run python manage.py makemigrations to generate the corresponding migration files
  • Commit these migration files along with your code changes and push everything to your Bitbucket repository
  • When deploying to your Ubuntu/DigitalOcean server:
    1. Pull the latest code (including all migrations)
    2. Run python manage.py migrate to apply the migrations to your production database
  • Critical note: Never run makemigrations on your production server unless you’re 100% sure no other developers have modified models, and your local migrations are fully synced to the repo. This is almost always a risky move that’s best avoided.

Quick .gitignore Tip

Make sure your ignore file doesn’t exclude the entire migrations folder—only exclude cache files. Add these lines to your .gitignore:

migrations/__pycache__/
migrations/*.pyc

内容的提问来源于stack exchange,提问作者Zorgan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:43:34