SQL to SQLAlchemy Models Converter

Paste any CREATE TABLE SQL and get SQLAlchemy 2.0 declarative models with Mapped[], mapped_column(), ForeignKey, and relationship() — ready to paste into your Python project.

Runs 100% in your browser. Your input and files never leave your device.

Why use our online SQL to SQLAlchemy Models Converter?

SQLAlchemy 2.0 introduced the Mapped[T] annotation syntax and mapped_column() API — a significant improvement over the legacy Column() style. Converting a CREATE TABLE statement to a modern SQLAlchemy model means mapping each SQL type to the right SQLAlchemy type class, writing Mapped[Optional[T]] for nullable columns, and adding ForeignKey and relationship() for each FK. This tool does it automatically.

How to use SQL to SQLAlchemy Models Converter

  1. 1
    Paste your SQL

    Paste one or more CREATE TABLE statements. Click the SQLAlchemy tab.

  2. 2
    Review the output

    The right panel shows a complete models.py using SQLAlchemy 2.0 DeclarativeBase, Mapped[], and mapped_column().

  3. 3
    Check the type mapping

    Expand the Type Mapping panel to verify every column — SQL type → Python type annotation + SQLAlchemy column type.

  4. 4
    Copy or download

    Click Copy to grab models.py, or Download ZIP for the full output.

Frequently Asked Questions

Does this use SQLAlchemy 1.4 or 2.0 style?

SQLAlchemy 2.0 style using DeclarativeBase, Mapped[T], and mapped_column(). This is the recommended modern approach. If you need 1.4 legacy style with Column() and declarative_base(), the output needs minor adjustments but the type mappings are the same.

How is TIMESTAMPTZ (timestamp with timezone) handled?

TIMESTAMPTZ maps to DateTime(timezone=True) which instructs SQLAlchemy to use a timezone-aware datetime column. The Python type annotation is datetime from the datetime module.

How are relationships generated?

Each foreign key generates a relationship() call with back_populates set to the referencing table name. You may need to adjust the back_populates value to match your actual reverse relation name.

Is JSONB supported for PostgreSQL?

Yes. JSONB maps to JSONB from sqlalchemy.dialects.postgresql (imported from sqlalchemy). The Python type annotation is dict.

What about array types like TEXT[]?

Array types use ARRAY(Text) from SQLAlchemy. The inner type is inferred from the SQL array element type (TEXT[] → ARRAY(Text), INTEGER[] → ARRAY(Integer)). ARRAY is a PostgreSQL-specific type and requires the pg dialect.

Also useful: Cron BuilderMermaid Live Editor