Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of sqlall in Django 1.9?

Tags:

python

django

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?

like image 589
Richard Avatar asked Feb 17 '16 11:02

Richard


2 Answers

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
like image 99
Selcuk Avatar answered Oct 06 '22 12:10

Selcuk


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.

like image 25
mlissner Avatar answered Oct 06 '22 12:10

mlissner