Developer and Text Tools

How to Convert SQL Tables into ORM Models with Correct Relations

Turn CREATE TABLE statements into Prisma, TypeORM, Sequelize, SQLAlchemy, Django or GORM models, with foreign keys mapped to relations on both sides.

Saizul Amin
Saizul Amin2 Nov 2026 · 4 min read
How to Convert SQL Tables into ORM Models with Correct Relations

How to Convert SQL Tables into ORM Models with Correct Relations

Adopting an ORM on an existing database means describing every table again, this time in code. Each column type, default value, key and constraint has to be translated, and every framework uses its own syntax. The trickiest part is relations: a foreign key in SQL is one line, but in an ORM it usually becomes two fields, one on each model, with names that must not clash. This guide explains how relational schemas map to ORM models and how to generate them reliably.

From tables to models

An ORM (object-relational mapper) represents each table as a class or model and each row as an object. Most of the mapping is mechanical:

  • Columns become fields, with SQL types translated to the language's types, for example VARCHAR(120) to a string with a length limit, NUMERIC(10,2) to a decimal and TIMESTAMP to a date-time.
  • Constraints become attributes: NOT NULL decides whether a field is optional, DEFAULT becomes a default value or a server default, UNIQUE becomes a uniqueness flag.
  • Keys become identity: single-column primary keys, auto-increment or identity columns, and composite primary keys spanning several columns.

How relations are mapped

A foreign key column such as posts.author_id REFERENCES users(id) describes a many-to-one relation: many posts, one author. In an ORM you usually want to navigate both ways, so the post gets an author field and the user gets a posts list. If the foreign key column is also unique, each user can have at most one such row and the relation becomes one-to-one.

Things get harder when a table references another table twice, for example a post with both an author and an editor. The ORM must be told which reverse field belongs to which foreign key, otherwise it reports an ambiguous relation. Each framework solves this differently: named relations in Prisma, related_name in Django, explicit foreign key arguments in SQLAlchemy.

ON DELETE behaviour also matters. CASCADE removes child rows when the parent is deleted; SET NULL keeps them and clears the reference. Preserving that in the model keeps the ORM's assumptions in line with the database.

Generating models with the tool

Open the SQL to ORM Model Generator.

  1. Paste the schema. It reads CREATE TABLE, ALTER TABLE ... ADD CONSTRAINT and CREATE INDEX statements from PostgreSQL, MySQL and MariaDB, SQLite and SQL Server, including quoted names and schema prefixes. Other statements are skipped with a note.
  2. Check What was read from the SQL. This view lists tables, columns, types, keys and the relations that were detected.
  3. Fix warnings, such as tables without a primary key or foreign keys pointing to tables that are not in the input.
  4. Choose the framework: Prisma, TypeORM, Sequelize, SQLAlchemy 2.0, Django or GORM.
  5. Set the database for Prisma so native type attributes match PostgreSQL, MySQL, SQLite or SQL Server.
  6. Copy or download the result, or press ZIP all six to get one file per framework.

Naming follows each ecosystem's conventions. Table names become singular PascalCase models, so categories becomes Category. JavaScript and TypeScript outputs use camelCase fields mapped back to the original column names; Python keeps column names; Go uses PascalCase with initialisms such as AuthorID. Every model keeps a mapping to the original table name, so the database itself does not need to change.

A worked example

The sample blog schema has four tables: users, posts, tags and post_tags. posts references users twice, as author_id with ON DELETE CASCADE and as editor_id with ON DELETE SET NULL. post_tags links posts and tags with a composite primary key made of both foreign keys.

The generator detects four relations. In Prisma it gives the two user relations distinct names, PostAuthor and PostEditor, and creates the reverse lists postsAsAuthor and postsAsEditor on User, so the schema is unambiguous. The composite key on post_tags is kept, and in Django it uses the CompositePrimaryKey field introduced in Django 5.2.

Tips and pitfalls

  • Feed it schema only. A dump that includes data inserts works, but a schema-only export is easier to review.
  • Review type choices. Money, time zones and large integers deserve a second look in your target language.
  • Decide on many-to-many. Join tables are generated as two many-to-one relations, which keeps extra columns accessible. Switch to your ORM's implicit many-to-many only if the join table has nothing else in it.
  • Validate before migrating. Run your ORM's formatter, type checker or schema validator, and compare a generated migration against the live database to be sure it would not change anything unexpectedly.

Parsing and generation run in your browser, so your schema is not uploaded.

Limits

Views, triggers, check constraints, generated columns, partitions and stored procedures are not converted, and vendor-specific types the tool does not recognise fall back to strings. Join tables are not turned into implicit many-to-many relations. The generator does not connect to a database, so it cannot read a live schema or verify that the output matches it. Treat the models as a strong first draft and review them with your ORM's own tools before running any migration.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages