Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying django migrations table

How can one query the django_migrations table from a view? For instance(What I have tried and of course not working)

from django.db import migrations

latest_migration = migrations.objects.all().order_by('-applied')[0]

If possible, how should one proceed?

like image 248
Olfredos6 Avatar asked Apr 30 '18 12:04

Olfredos6


People also ask

What is Django migration table?

Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations. For each migration that's applied or faked, a new row is inserted into the table. For example, here's what this table looks like in our bitcoin_tracker project: ID.

How do I see unapplied migrations?

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.

How do I see previous migration in Django?

You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration. 0011_migration_to_revert.


1 Answers

The migration model is defined in django.db.migrations.recorder. So you can should change your example code slightly:

from django.db.migrations.recorder import MigrationRecorder
latest_migration = MigrationRecorder.Migration.objects.order_by('-applied')[0]
like image 130
Alasdair Avatar answered Oct 27 '22 02:10

Alasdair