Generating SQL for Django Apps

December 3, 2010

Sometimes, you don't have direct access to the database and so can't rely on Django creating tables with syncdb.

Normally, you can run

$ ./manage.py sql appname

To generate the SQL to create tables for the appname app.

I was looking at the documentation and trying to work out how to do this for the standard Django tables such as auth_user and django_site. ./manage.py sql django.contrib.auth didn't work, giving:

Error: App with label django.contrib.auth could not be found. Are you sure your INSTALLED_APPS setting is correct?

Thanks to the ever-helpful Django mailing list I was able to find out quickly how to do this.

I needed to run the following commands to generate the create table (and sequence - I was using Oracle) scripts required:

$ ./manage.py sql auth
$ ./manage.py sql sites
$ ./manage.py sql sessions
$ ./manage.py sql admin
$ ./manage.py sql contenttypes