1

I've created a web application using Django. I'm not very happy with it, and would like to export/translate the whole Django model I've created as an SQL statement.

I know the following statement prints the an SQL statement for incremental changes to the database:

python manage.py sqlmigrate polls 0001 

But how to I export the initial (whole) model into an sql file?

Theoretically, I could simply export an SQL dump directly from the database. But my database has become messy (additional tables) and my models.py is a clean formulation of the original database.

1
  • As far as I know, the python manage.py sql command that did what you want is deprecated since Django 1.8 Commented Jan 17, 2017 at 21:40

1 Answer 1

4
  1. Rename your migrations folder to foo so Django can't find it (or delete it altogether, if you're brave)
  2. Delete or rename the django_migrations table
  3. python manage.py migrate --fake
  4. python manage.py makemigrations polls
  5. python manage.py sqlmigrate polls 0001
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't seem to work. After the first command, it says No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them. After the second: CommandError: App 'mainapp' does not have migrations
@Ratnanil I think I had missed a step. You might need to rename your django_migrations table
faking a migration now works, but the error ComandError: App... still appears. Do you have any idea why?
@Ratnanil I guess if you've moved the migrations folder and renamed the column in the database, you can just create a new initial migration as usual with makemigrations and then use sqlmigrate to see the SQL statement. let me know if that works and I'll edit the answer. unfortunately, I'm doing this from memory as I'm not on a django-powered machine atm
yes it did! After step 3 insert python manage.py makemigrations mainapp and then python manage.py sqlmigrate mainapp 0001. Update your answer and I will gladly accept. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.