tags:
- django
- django-migrations
- python
- database
title: How to migrate Django databases
permalink: how-to-migrate-django-databases
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
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
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
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 to a previous migration.
python manage.py migrate users
# 0009_remove_hierarchy_level1_and_more