What is a database migration?

In short
A database migration is a saved, versioned set of instructions that changes a database's structure, such as adding a column or a table. Developers write migrations alongside their code so every machine and server can update its database the same way. Django is one framework that creates and applies them for you.
A database migration is a small file that describes one change to your database structure. The structure is called the schema. It includes tables, columns, and the links between tables. When your application changes, its database often has to change too. A migration is the safe, repeatable way to make that happen.
Why migrations exist
Imagine you add a new field to a model in your code. The database table behind that model does not have the new column yet. Someone must add it. You could write the SQL by hand and run it on every machine, but that is easy to get wrong. Migrations solve this by storing each change in a file, in order. Anyone who runs the files gets the same result.
The Django documentation describes migrations as a version control system for your database schema. Think of each migration as a commit. It packages one set of changes, and it can be applied to a database that has not seen it yet.
How it works
Migrations usually involve two steps. First, you create a migration file from the changes you made in your code. Then, you apply that file to the database. In Django, the commands are:
- makemigrations, which creates new migration files from changes to your models.
- migrate, which applies migrations to the database.
- sqlmigrate, which shows the SQL a migration would run, without running it.
- showmigrations, which lists the migrations and whether each one has been applied.
A typical session looks like this:
$ python manage.py makemigrations $ python manage.py migrate
The first command writes a file into the app's migrations folder. The second command runs that file against your database. Django keeps track of which migrations have already run, so running migrate again does nothing new.
The migration files should be committed to version control with the model changes. Developers, staging servers, and production servers then run the same files in the same order. Django's documentation says this makes development and production behave the same way for the same data.
A simple example
Suppose you have a Book model with a title and an author. You decide to add a page count. You add the field to the model, then run makemigrations. Django writes a new file. You read the output, which tells you what it detected, and then run migrate. The new column appears in the database.
Before running it for real, you can look at the SQL with sqlmigrate. This is useful if a database administrator needs to review the change first. The command prints the statements but does not touch your data.
Common mistakes
A few problems come up often. Read the output of makemigrations before you apply it, because it is not perfect and may miss complex changes. Do not edit an applied migration. Instead, create a new one. Commit the model change and its migration together so that other developers receive both at once.
When two developers create migrations at the same time, you can end up with two files that have the same number. Django only needs each name to be unique, but it may ask you to combine them in a sensible order.
Be careful with databases that cannot roll back schema changes. The Django documentation notes that MySQL does not run schema changes inside a transaction. If a migration fails there, you may need to undo the partial changes by hand. SQLite is useful for local development, but the documentation advises against running it in production unless you know its limits well.
Alternatives
Some teams write raw SQL scripts and track them by hand. This can work for small projects, but it puts the burden of ordering and tracking on people. Many other frameworks offer their own migration tools with a similar idea: a versioned file per change. If you are learning, start with the tool your framework provides. The core concept stays the same wherever you use it.
To learn more about the framework side, read the Django migrations documentation linked below. It covers dependencies, data migrations, and non-atomic migrations in more depth.
FAQ
Do I need to write migrations by hand?
Often not. Django can create most migrations automatically from your model changes. You may still need to edit one for complex cases, such as moving data between columns.
Should I edit a migration after it has been applied?
No. Other databases have already run it, so changing it can cause them to drift out of sync. Create a new migration for the next change instead.
What happens if a migration fails halfway?
It depends on the database. PostgreSQL and SQLite can roll back the whole migration in one transaction. MySQL cannot, so you may need to fix the partial changes manually before trying again.
Can I see the SQL before a migration runs?
Yes. Django's sqlmigrate command prints the SQL for a migration without applying it to your database.
Sources
This page is based on the official documentation below. Read it for full detail.
Database migration — official documentation ↗