SQL to SQLAlchemy Models

Paste CREATE TABLE SQL and get SQLAlchemy Python model classes instantly. Choose between SQLAlchemy 2.0\'s Mapped + mapped_column style (the modern default) or the classic Column approach. The converter maps SQL types to the right SQLAlchemy column types, applies primary_key, nullable, unique, and wires ForeignKey() from SQL REFERENCES clauses.

How to use the SQL to SQLAlchemy Models

Paste one or more CREATE TABLE statements (separated by semicolons) into the input box. Select the SQLAlchemy style you want — 2.0 Mapped uses Mapped[int] type annotations and mapped_column(), which is the recommended style for all new projects using SQLAlchemy 2.x. Classic Column uses the id = Column(Integer, primary_key=True) form compatible with SQLAlchemy 1.x and older tutorials.

Click Generate Models. The output is a Python module with a Base class and one model class per table. Each column is mapped to the appropriate SQLAlchemy type: INTEGER/SERIALInteger, VARCHAR(n)/CHAR(n)String(n), TEXTText, BOOLEANBoolean, TIMESTAMP/DATETIMEDateTime, DECIMAL(p,s)Numeric(p,s), JSONB/JSONJSON, BYTEA/BLOBLargeBinary.

FOREIGN KEY table constraints are picked up and rendered as ForeignKey("table.col") in the column definition. Use the Example button to load a two-table schema and see the output before pasting your own SQL.

What is SQLAlchemy?

SQLAlchemy is the most widely-used Python SQL toolkit and ORM. Its two main layers — the Core (expression language) and the ORM — give you fine-grained control over SQL while optionally providing a high-level object-mapping layer. Version 2.0, released in 2023, introduced the Mapped generic type annotation system which makes model definitions fully typed and introspectable by type checkers and IDE tools. If you are starting a new project or upgrading from 1.x, the 2.0 Mapped style is the recommended path.

In the 2.0 style a model column is declared as a class attribute with a Mapped[T] annotation and a mapped_column() assignment. The type annotation drives nullability — Mapped[int] is NOT NULL, Mapped[Optional[int]] is nullable. In the classic style the annotation is dropped and you write Column(Integer, ...) directly, which is compatible back to SQLAlchemy 1.0. Both styles work with the same declarative_base() / DeclarativeBase mechanism.

This converter handles the most common column types and constraints but does not emit relationship back-references (relationship()), column-level check constraints, or database-specific types like ARRAY or TSVECTOR. Those need to be added manually after conversion.

Common use cases

  • Bootstrapping a new project — start from an existing SQL schema (from a DBA, a data warehouse, or a third-party tool) and get working Python model classes in seconds.
  • Database reverse-engineering — dump CREATE TABLE from a legacy database and generate the ORM layer without writing boilerplate by hand.
  • Learning SQLAlchemy — see how SQL column types and constraints map to SQLAlchemy constructs; compare the classic and 2.0 styles side-by-side.
  • Multi-ORM projects — use the SQL DDL as the canonical schema definition (possibly generated from a Prisma schema) and generate models for different ORMs.
  • Code generation pipelines — integrate the pattern into a CI step that keeps SQLAlchemy models in sync with the SQL schema checked into version control.

Frequently asked questions

Which SQLAlchemy version does the output target?

The "2.0 Mapped" style targets SQLAlchemy 2.0+. The "Classic Column" style is compatible with SQLAlchemy 1.x through 2.x (the classic style is still supported in 2.x for backwards compatibility).

Does it generate relationship() back-references?

No — relationship() requires knowing the back-reference name and cardinality, which cannot be reliably inferred from a FOREIGN KEY constraint alone. You will need to add those manually.

How does it handle composite primary keys?

Table-level PRIMARY KEY (col1, col2) constraints are parsed and primary_key=True is applied to each named column, producing a composite PK in SQLAlchemy.

What about AUTO_INCREMENT / SERIAL?

SERIAL, BIGSERIAL, and AUTO_INCREMENT are detected and the column gets Integer (or BigInteger) with primary_key=True. SQLAlchemy's server-side default handles the auto-increment automatically when primary_key=True is set on an Integer column.

Does it support MySQL and SQL Server syntax?

Yes. The parser handles backtick-quoted identifiers (MySQL), double-quoted identifiers (Postgres / SQL Server), and common type aliases like TINYINT(1) (mapped to SmallInteger) and NVARCHAR (mapped to String).