SQL DDL to Django Models

Converts CREATE TABLE statements into Django models.py code with max_length, decimal precision, null and blank, unique, defaults, ForeignKey and db_table so the models map onto the existing tables.

Use the tool

Why this tool exists

Django ships with inspectdb, but it needs a configured project and a live database. When you only have SQL, for example a dump from a legacy system, you must write the models yourself. This tool generates them from the DDL: the right field class for each column, max_length for varchar, decimal precision, null and blank, unique, defaults and ForeignKey with on_delete. It sets db_table and db_column so the models point at the existing tables instead of creating new ones. The implicit id AutoField is left out, and composite primary keys become unique_together because Django has no composite keys. Review and set managed = False if the tables are not yours to migrate. It runs in your browser.

How to use it

  1. Paste your CREATE TABLE statements into the input box.
  2. Run the conversion to get the model classes.
  3. Copy them into models.py, check field types and on_delete choices, and decide whether the models should be managed by Django.

Frequently asked questions

Why is the id field missing?

Django adds an implicit auto-incrementing id, so a simple integer primary key called id is omitted. Other primary keys are written explicitly.

How are composite primary keys handled?

Django does not support them, so the columns become a unique_together constraint. You may need to designate one field as the primary key by hand.

Will the models match my existing tables?

They use db_table and db_column, so yes in naming. Set managed = False in Meta if migrations should not touch the tables.

How are foreign keys written?

As ForeignKey fields with an on_delete value chosen from the DDL. Check the target model and on_delete behaviour against your intent.