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?
Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.
You can selectively disable migration for one or more Django-Models by setting managed = False in Django Model Meta options.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With