What is SQL to SQLModel Generator?
SQLModel was created by Sebastián Ramírez, the author of FastAPI, to solve a persistent problem in Python web development: maintaining separate classes for database persistence and API validation. A SQLModel class annotated with table=True is simultaneously a SQLAlchemy model that can create and query database rows, and a Pydantic model that validates incoming request data and serializes responses. This converter reads your SQL CREATE TABLE definitions and produces SQLModel classes with precise Python type annotations, Field() calls for primary keys and foreign keys, sa_column overrides for types that need SQLAlchemy-specific column configuration, and optional separate Create and Read schemas that omit auto-generated fields like id from API input. The generated code integrates directly with both FastAPI endpoints and SQLAlchemy sessions without any adapter layer.
How to Use
- Paste your CREATE TABLE statements into the input area
- Select your Python version — 3.9+ uses Optional[X] syntax while 3.10+ supports the X | None union syntax
- Toggle the table=True checkbox to include SQLAlchemy table mapping, and the Pydantic validation checkbox to generate Create/Read schemas
- Choose whether nullable columns should use Optional[T] or T | None notation
- Click Generate SQLModel and copy the output into your Python project
Why Use This Tool?
Tips & Best Practices
- A SQLModel class with table=True creates an actual database table. Without table=True, it is a pure Pydantic schema for validation only
- Use the Pydantic validation option to generate separate Create and Read models — this is the recommended pattern for FastAPI applications where you want to exclude the id field from creation payloads
- Python 3.10+ union syntax (X | None) is cleaner but requires your deployment environment to support it. Stick with Optional[X] if you need Python 3.9 compatibility
- Install SQLModel with pip install sqlmodel — it includes both Pydantic and SQLAlchemy as dependencies
Frequently Asked Questions
How are SQL types mapped to Python types in SQLModel?
INTEGER, BIGINT, and SERIAL map to int, VARCHAR and TEXT map to str, BOOLEAN maps to bool, TIMESTAMP and DATETIME map to datetime, DATE maps to date, FLOAT and REAL map to float, DECIMAL and NUMERIC map to Decimal, UUID maps to uuid.UUID, JSON and JSONB map to dict, and BLOB maps to bytes. Nullable columns use Optional[T] or T | None depending on your Python version selection.
When should I avoid SQLModel and use plain SQLAlchemy or Pydantic separately?
SQLModel is ideal for FastAPI projects that need both validation and persistence in one class. If you are building a data pipeline that only needs SQLAlchemy, or a pure API that only needs Pydantic validation without a database, using them separately gives you more control. SQLModel also lags behind SQLAlchemy and Pydantic in adopting their latest features, so cutting-edge needs may require direct usage.
What does the Pydantic validation option generate?
It creates two additional classes per table: a ClassNameCreate model that omits auto-increment primary key fields (since the database generates them), and a ClassNameRead model that includes all fields with nullable columns marked as Optional. These classes are pure Pydantic models (no table=True) suitable for FastAPI request body and response model typing.
How does sa_column work in the generated code?
For SQL types that need SQLAlchemy-specific configuration — such as String(255) with a length parameter, Numeric(10, 2) with precision and scale, JSON() for JSON columns, or Uuid() for UUID columns — the converter generates sa_column=Column(...) overrides. This tells SQLModel to use the exact SQLAlchemy column definition instead of inferring it from the Python type annotation.
Is my SQL schema transmitted to any server during conversion?
No. The entire conversion process runs in your browser using a client-side parser. Your database schema is never sent over the network or stored on any external server.
Can I use the generated models with Alembic migrations?
Yes. SQLModel classes with table=True are fully compatible with Alembic's autogenerate feature. Run alembic revision --autogenerate after adding the generated models to your application, and Alembic will detect the new tables and columns automatically.
Real-world Examples
FastAPI application with user authentication
A FastAPI project needs User and Session models that serve as both database tables and API schemas. The developer generates SQLModel classes with Create/Read variants, then uses them directly in endpoint signatures.
E-commerce product catalog with decimal pricing
A product catalog stores prices as DECIMAL for financial precision. The converter generates SQLModel classes with sa_column overrides for Numeric columns and String lengths.