I'm working in Django 1.9, and I'm trying to get the SQL that would be used to create database tables from models.py.
It seems that in previous version of Django one could do:
python manage.py sqlall <app_name>
But that no longer seems to be the case. I just get Unknown command: 'sqlall'
. Nothing in the docs seems to offer this.
How can I see how my models are turned into SQL?
It is deprecated in Django 1.9:
The SQL management commands for apps without migrations, sql, sqlall, sqlclear, sqldropindexes, and sqlindexes, will be removed.
The closest command you can use is sqlmigrate:
Prints the SQL for the named migration. This requires an active database connection, which it will use to resolve constraint names; this means you must generate the SQL against a copy of the database you wish to later apply it on.
You can use the migration name 0001_initial
for the creation script, for example:
python manage.py sqlmigrate myapp 0001_initial
I found that using sqlmigrate was a pretty rotten solution for me. I'm setting up logical replications, and so I need the SQL of the database, not the SQL of individual migrations.
The solution I found was to use pg_dump
with the --schema-only
option.
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