4

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 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.