MySQL CREATE TABLE to SQLAlchemy model
Paste a MySQL CREATE TABLE and get a SQLAlchemy 2.0 model with typed Mapped[...] columns. AUTO_INCREMENT becomes the generated primary key, TINYINT(1) maps to bool, DECIMAL to Decimal, and nullable and unique constraints carry over from the DDL.
Generated from your real schema, on the server, discarded after the response.
Example
CREATE TABLE `product` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`sku` VARCHAR(40) NOT NULL,
`name` VARCHAR(200) NOT NULL,
`price` DECIMAL(12, 2) NOT NULL,
`in_stock` TINYINT(1) NOT NULL DEFAULT 1,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_product_sku` (`sku`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
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)