Modifying Old Django Migration Files: Safe or Not?
In this article
- Why Django Migrations Are Append-Only
- How Django Tracks Applied Migrations
- When Editing Is Acceptable
- What to Do Instead of Editing Old Migrations
- Common Problems Caused by Editing Old Migrations
- How to Recover If You Already Edited an Applied Migration
- Best Practices for Managing Migrations
- Frequently Asked Questions
No, it is generally not correct to modify old migration files in Django once they have been applied to any environment, including your local development database. The only safe exception is when the migration has never been applied anywhere and you are still in the initial development phase before the first deploy.
Why Django Migrations Are Append-Only
Django’s migration system is designed to be a linear, append-only history of schema changes. Each migration file records a sequence of operations that transform the database from one state to the next. Django tracks which migrations have been applied by storing their names in the django_migrations table. If you edit a migration that has already been recorded there, the framework will not detect the change—it will simply see that the migration is already applied and skip it. This means your edited schema changes will never be executed, and your database will silently diverge from your code’s expected state.
Even worse, if you edit a migration and then run makemigrations again, Django may generate a new migration that conflicts with the edited one, leading to duplicate operations or missing dependencies. The entire history becomes inconsistent, and you can end up with a database that cannot be migrated forward or backward reliably.
How Django Tracks Applied Migrations
When you run migrate, Django reads the migration files in order, checks the django_migrations table to see which ones are already applied, and runs only the missing ones. Each applied migration is recorded with its app label and name. This record is the source of truth—not the file contents. So if you edit a migration file after it has been applied, Django will not re-run it, because the record already exists.
In a team environment, the problem is amplified. If one developer edits an old migration and commits it, other developers who already applied the original version will not get the change. Their databases will remain in the old state, while the codebase expects the new state. This causes hard-to-debug errors that only appear in certain environments.
When Editing Is Acceptable
There is one clear situation where editing an old migration is safe: when that migration has never been applied to any database, including your own local one. This typically happens during the initial development phase before the first deploy. If you are still designing your models and have not run migrate on any persistent environment, you can freely edit or delete migration files. However, you must also delete the corresponding entry from the django_migrations table if you have already applied it locally—or simply reset the database and start fresh.
Another acceptable scenario is when you are working on a feature branch and the migration is new, unmerged, and unapplied by anyone else. In that case, you can squash or edit it before merging. But once the migration is merged and deployed, treat it as immutable.
What to Do Instead of Editing Old Migrations
If you need to change a schema that is already covered by an applied migration, the correct approach is to create a new migration that alters the schema. For example, if you originally created a Book model with a title field and later decide to make it unique, you should not edit the original 0001_initial.py. Instead, run:
- Modify the model in
models.pyto addunique=True. - Run
python manage.py makemigrations—Django will generate a new migration file, e.g.,0002_alter_book_title.py. - Run
python manage.py migrateto apply it.
This keeps the migration history linear and ensures that every environment—production, staging, and other developers’ machines—can apply the same set of changes in the same order.
Common Problems Caused by Editing Old Migrations
Developers often edit old migrations to fix a mistake or to add a field they forgot. The typical consequences include:
- Silent schema drift: The database does not match the models, causing runtime errors like
OperationalError: no such columnorcolumn does not exist. - Broken migration dependencies: Later migrations may reference fields or tables that no longer exist in the edited migration, causing
django.db.migrations.exceptions.InconsistentMigrationHistory. - Merge conflicts: In a team, editing an old migration often leads to conflicting changes in the same file, which are painful to resolve.
- Data loss: If you edit a migration that originally created a table and then remove a column, the data in that column is lost when the migration is re-applied on a fresh database—but existing databases keep the column, creating inconsistency.
How to Recover If You Already Edited an Applied Migration
If you have already edited an applied migration and are facing issues, the safest recovery is to revert to the original migration content from version control, then create a new migration for any changes you need. If you have already run migrate with the edited file, you may need to manually fix the database or roll back to a known good state. In a development environment, you can use python manage.py migrate to roll back to a previous migration, but this only works if the migration history is still consistent. In production, you should never attempt to roll back a migration that has been applied—instead, create a new migration that reverses the unwanted change.
Best Practices for Managing Migrations
- Treat applied migrations as immutable: Once a migration is committed and applied anywhere, never edit it.
- Use
makemigrationsfor every model change: Let Django generate the migration files automatically. - Review migrations before applying: Check the generated file to ensure it does what you expect.
- Keep migrations small and focused: Avoid bundling unrelated schema changes into one migration.
- Use
squashmigrationsfor cleanup: If your migration history becomes too long, you can squash it into a single migration, but only after the old ones have been applied everywhere.
Frequently Asked Questions
Can I edit a migration if I haven’t deployed yet?
Yes, if the migration has never been applied to any database other than your local one, and you are willing to reset your local database, you can edit or delete it. Just make sure no one else has applied it either.
What if I already edited an old migration and now my tests fail?
Tests often recreate the database from migrations, so if you edited an old migration, the test database will use the edited version, which may not match your models. The fix is to revert the migration to its original state and create a new migration for your changes.
Is it okay to edit a migration that is not applied in production but is in a shared branch?
If the migration has been committed to a shared branch and other developers have pulled it, they may have already applied it locally. Editing it would cause inconsistency. It is safer to create a new migration instead.
How do I fix a mistake in a migration that has already been applied?
Create a new migration that reverses the mistake. For example, if you added a column that should not exist, use RemoveField in a new migration. Do not edit the original file.
Can I use squashmigrations to clean up old migrations?
Yes, squashmigrations is a safe way to consolidate a long history into a single migration, but only after all the old migrations have been applied to all environments. The squashed migration is treated as a new migration, and the old ones remain for historical reference.