What is SQL to Tortoise ORM Model Generator?
Tortoise ORM brings the familiar Django ORM developer experience to Python's async ecosystem. Built from the ground up for asyncio, it lets you define models with declarative field types, query them using await Model.filter() and await Model.get() syntax, and traverse relationships with prefetch_related and fetch_related. This converter reads your SQL CREATE TABLE statements and produces Tortoise ORM model classes with the correct field types from the fields module, including IntField, CharField with max_length, DatetimeField with auto_now_add for timestamps, DecimalField with max_digits and decimal_places, and ForeignKeyField for references. Optional Meta classes specify the database table name, and Pydantic schema generation via generate_schema() gives you ready-made serializers for your API layer.
How to Use
- Paste your CREATE TABLE statements into the SQL input area
- Select the database dialect to ensure correct type interpretation
- Toggle options: include Pydantic schemas for API serialization, __str__ methods for readable representations, and Meta classes for explicit table names
- Click Generate Tortoise ORM Models and review the Python output
- Copy the generated models into your async Python project and register them in your Tortoise initialization config
Why Use This Tool?
Tips & Best Practices
- Tortoise ORM uses the fields module for all column definitions — every field type is explicitly declared rather than inferred from Python type annotations
- Foreign key columns ending in _id are automatically converted: the _id suffix is removed and a ForeignKeyField is created with the related model name derived from the referenced table
- Use Aerich, the official Tortoise migration tool, to generate and apply database schema migrations after creating your models
- Nullable columns without NOT NULL constraints receive null=True automatically, and default values from SQL are translated to the default parameter on the field
Frequently Asked Questions
How does the converter map SQL types to Tortoise ORM field types?
INTEGER and SERIAL map to IntField, BIGINT and BIGSERIAL map to BigIntField, SMALLINT maps to SmallIntField, VARCHAR maps to CharField with max_length, TEXT maps to TextField, BOOLEAN maps to BooleanField, TIMESTAMP maps to DatetimeField, DATE maps to DateField, DECIMAL maps to DecimalField with max_digits and decimal_places, FLOAT maps to FloatField, UUID maps to UUIDField, JSON maps to JSONField, and BLOB maps to BinaryField.
When should I avoid Tortoise ORM and use a synchronous ORM instead?
If your application is built on a synchronous framework like Flask or Django, Tortoise's async-first design adds unnecessary complexity. For Django projects, use the built-in ORM. For Flask with synchronous patterns, SQLAlchemy with Flask-SQLAlchemy is more straightforward. Tortoise shines in async contexts like FastAPI, Starlette, or Sanic where non-blocking database calls are essential.
How are foreign key relationships represented?
SQL REFERENCES clauses are converted to ForeignKeyField definitions. For example, author_id INTEGER REFERENCES users(id) becomes author = fields.ForeignKeyField("models.User", related_name="posts_set"). The _id suffix is stripped from the field name, and the related model name is derived from the referenced table name in singular form.
What does the Pydantic schema option generate?
It appends a ClassName_Pydantic = ClassName.generate_schema() line for each model. This creates a Pydantic model that mirrors the Tortoise model's fields, suitable for FastAPI response serialization. You can customize the generated schema by passing configuration to generate_schema() in your code.
Is my SQL schema sent to any external server?
No. The conversion runs entirely in your browser using a client-side regex parser. Your database schema is never transmitted over the network at any point.
Can I use the generated models with Aerich migrations?
Yes. After copying the generated models into your project, initialize Aerich with aerich init, then run aerich init-db to create the initial migration. Subsequent model changes can be detected with aerich migrate --name description and applied with aerich upgrade.
Real-world Examples
Async blog platform with FastAPI and Tortoise ORM
A blog platform uses FastAPI for the API layer and Tortoise ORM for async database access. The developer converts the existing SQL schema to Tortoise models, then uses await-based queries in route handlers.
Inventory management with decimal quantities
A warehouse tracking system stores product quantities and prices with decimal precision. The converter generates DecimalField with the correct max_digits and decimal_places from the SQL NUMERIC type parameters.