Django Commands: python sql your_app

By: Dusty Arlia
Published on Friday, February 14, 2014, 06:12 PM
Last Updated on Tuesday, July 14, 2015 at 3:41 PM
Total Updates: 3

The python sql your_app command prints what SQL Django thinks is required to generate your database tables to the screen. You can copy and paste this SQL into your database prompt to commit the SQL to the database.

The python sql your_app command is smart enough to automatically generate the proper database table creation commands, using the correct syntax. The names of the tables are your app name followed by an underscore and your lowercase model name. For example:


You can edit this, it is just the naming convention that Django defaults to.

By default, a primary key field is also added to the tables automatically (this can be changed). The name of this field is id. If there is a foreign key field in your table, Django will append _id to the field name by default (this can be changed). If you are using SQLite, the foreign key relationship is made explicit using a REFERENCES statement.

The field types are automatically choosen based upon your database-type (SQLite, MySQL, PostgreSQL, Oracle, etc.). And your feild names are either single-quoted or double-quoted based on your database type.