Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - view sql query without publishing migrations

Tags:

django

When I make changes to some models, I want to view the SQL the django would be running to implement those changes on the DB.

The normal way of doing this would be to do 'makemigrations appname'. This will generate a migration, let's say, - '0001_someName.py'. Then one can do 'sqlmigrate 0001_someName.py'

But I want to view the sql directly, without having to create that intermediate migration. Can this be done?

like image 423
mayank mahajan Avatar asked Mar 23 '15 18:03

mayank mahajan


People also ask

Is Django migration necessary?

Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.

How do I stop migrations in Django?

You can selectively disable migration for one or more Django-Models by setting managed = False in Django Model Meta options.

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

How do I stop last migration in Django?

MIGRATION_NUMBER_PREFIX is the number prefix of the migration you want to revert to, for instance 0001 to go to 0001_initial.py migration. then you can delete that migration. You can use zero as your migration number to revert all migrations of an app.


2 Answers

Use the sqlmigrate command from manage.py.

python manage.py sqlmigrate <appname> <migration number eg. 0001 or 0004>

will display the SQL statements for a specific migration of the app.

like image 175
Amit Sharma Avatar answered Oct 11 '22 23:10

Amit Sharma


Django does not provide that option. You can always create the migration, run sqlmigrate, and delete the migration file. As long as it isn't applied with migrate, nothing will happen.

like image 5
knbk Avatar answered Oct 12 '22 00:10

knbk