How does django know which migrations have been run? How does django know which migrations have been run? python python

How does django know which migrations have been run?


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.


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 showmigrationsadmin [X] 0001_initial [X] 0002_logentry_remove_auto_addauth [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_messagescontenttypes [X] 0001_initial [X] 0002_remove_content_type_namesessions [X] 0001_initial


As other answers state, django has a special table django_migrations where it keeps the migration history.

If you are interested in digging a lit bit more, see MigrationRecorder class that is responsible for recording migrations in the database. Also, here is the underlying model for django_migrations table:

class Migration(models.Model):    app = models.CharField(max_length=255)    name = models.CharField(max_length=255)    applied = models.DateTimeField(default=now)    class Meta:        apps = Apps()        app_label = "migrations"        db_table = "django_migrations"    def __str__(self):        return "Migration %s for %s" % (self.name, self.app)