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

$ ./ 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. ./ 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:

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