Django database migrations

Create your new models in the app's models.py file.

Make the migrations. Django will detect the new models and create the migration files.

python manage.py makemigrations ${DJANGO_APP_NAME}

Migrate (create or update) the database.

python manage.py migrate

Example

Create new database models and tables for the users app.

python manage.py makemigrations users
python manage.py migrate users

You may also migrate all apps.

python manage.py migrate

View migrations

List all site migrations.

python manage.py showmigrations

List migrations for the users app.

python manage.py showmigrations users

List all unapplied migrations for the users app.

python manage.py migrate --plan users

Fake migrations

You may inherit a Django site that has not used database migrations. If you want to start using them, generate the migrations and "fake" them to fast forward to a good starting place.

python manage.py makemigrations
python manage.py migrate --fake 

After faking a migration, you may have to create manual migrations to get the database to a good spot. For example, you may need to drop a table.

python manage.py makemigrations --empty

This generates an empty migration file. You can now fill it in with the commands you need with migrations.RunSQL().

Revert migration

Revert to a previous migration.

python manage.py migrate users
# 0009_remove_hierarchy_level1_and_more
Django database migrations
Interactive graph
On this page
Example
View migrations
Fake migrations
Revert migration