Is there any way in django to generate the CREATE TABLE script for my models for a particular DB engine? (MySQL in my case)
2 Answers
you can use command in console
python manage.py sqlmigrate <appname> migration_name
where migration name is the name of file with migration that you need. e.g.
python manage.py sqlmigrate manager 0001_initial
1 Comment
Martin Massera
thanks! that's useful. But what if the table has been created and modified in many migrations? I guess I just can temporarily delete all migrations, create a new migration for everything, and then run this and extract the table I want.
if you want to smartly compress your migrations and get (maybe) your full sql script in one shot, you can run squashmigrations before sqlmigrate
python manage.py squashmigrations app_label [start_migration_name] migration_name
Squashes the migrations for app_label up to and including migration_name down into fewer migrations, if possible. The resulting squashed migrations can live alongside the unsquashed ones safely.