PostgreSQL CREATE TABLE to SQLAlchemy model
Paste a PostgreSQL CREATE TABLE and get a SQLAlchemy 2.0 model with typed Mapped[...] columns. Primary key, nullability and unique constraints carry over from the DDL, and NUMERIC maps to Decimal.
Generated from your real schema, on the server, discarded after the response.
Example
CREATE TABLE product (
id BIGSERIAL PRIMARY KEY,
sku VARCHAR(40) UNIQUE NOT NULL,
name VARCHAR(200) NOT NULL,
price NUMERIC(12, 2) NOT NULL,
in_stock BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMP NOT NULL DEFAULT now()
);
from datetime import datetime
from decimal import Decimal
from sqlalchemy import BigInteger, Boolean, DateTime, Numeric, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Product(Base):
__tablename__ = "product"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
sku: Mapped[str] = mapped_column(String(40), nullable=False, unique=True)
name: Mapped[str] = mapped_column(String(200), nullable=False)
price: Mapped[Decimal] = mapped_column(Numeric(12, 2), nullable=False)
in_stock: Mapped[bool] = mapped_column(Boolean, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)