Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does django know which migrations have been run?

How does django know whether a migration has been applied yet? It usually gets it right, but when it doesn't I don't ever know where to start troubleshooting.

like image 785
BostonJohn Avatar asked Dec 11 '14 19:12

BostonJohn


People also ask

How does Django migrate work?

The migrate command takes all the migrations that haven't been applied (Django tracks which ones are applied using a special table in your database called django_migrations) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.

Does Django test run migrations?

The Django test runner begins each run by creating a new database and running all migrations in it. This ensures that every test is running against the current schema the project expects, but we'll need to work around this setup in order to test those migrations.

How can I see unapplied migrations in Django?

You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. How can I find out which migrations are unapplied without running migrate? One way to do this is to look at the django_migrations table in the DB and check which ones are applied.


2 Answers

Django writes a record into the table django_migrations consisting of some information like the app the migration belongs to, the name of the migration, and the date it was applied.

like image 157
mipadi Avatar answered Sep 25 '22 21:09

mipadi


You can simply use showmigrations command to provide a list of migrations

$ python manage.py showmigrations 

whether or not each migration is applied (marked by an [X] next to the migration name).

~/workspace $ python manage.py showmigrations admin  [X] 0001_initial  [X] 0002_logentry_remove_auto_add auth  [X] 0001_initial  [X] 0002_alter_permission_name_max_length  [X] 0003_alter_user_email_max_length  [X] 0004_alter_user_username_opts  [X] 0005_alter_user_last_login_null  [X] 0006_require_contenttypes_0002  [X] 0007_alter_validators_add_error_messages contenttypes  [X] 0001_initial  [X] 0002_remove_content_type_name sessions  [X] 0001_initial 
like image 32
Deano Avatar answered Sep 25 '22 21:09

Deano