SQL to Tortoise ORM Model Generator

Convert SQL CREATE TABLE statements to Python Tortoise ORM model classes with proper type mapping, field constraints, and async support.

What is SQL to Tortoise ORM Model Generator?

This tool converts SQL CREATE TABLE statements to Python Tortoise ORM model classes. It generates async-compatible model definitions with proper field type mapping, constraints, foreign key relationships, and optional Pydantic schemas.

How to Use

  1. Paste your SQL CREATE TABLE statements
  2. Select your database dialect
  3. Configure generation options (Pydantic schemas, __str__ method, Meta class)
  4. Click "Generate" and copy the Tortoise ORM models

Why Use This Tool?

Generate async-compatible Tortoise ORM model classes
Map SQL types to Tortoise ORM field types automatically
Auto-detect foreign keys and create ForeignKeyField definitions
Optional Pydantic schema generation for API serialization
Support PostgreSQL, MySQL, and SQLite dialects

Tips & Best Practices

  • Tortoise ORM uses fields module for all column type definitions
  • Nullable columns (without NOT NULL) get null=True automatically
  • Foreign key columns ending with _id are converted to ForeignKeyField with the _id suffix removed
  • Use aerich for database migrations after generating models

Frequently Asked Questions

What is Tortoise ORM?

Tortoise ORM is an async ORM for Python, inspired by Django ORM. It provides an intuitive and familiar API for async database operations, supporting PostgreSQL, MySQL, SQLite, and other databases through aerich for migrations.

How are SQL types mapped to Tortoise ORM fields?

SQL types are mapped to Tortoise ORM field types. INTEGER maps to IntField, BIGINT maps to BigIntField, 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, and FLOAT maps to FloatField.

Does it generate Pydantic models too?

Yes, the tool can optionally generate Tortoise ORM Pydantic model schemas using the generate_schema() pattern for API serialization and validation.

How are foreign keys handled?

Foreign key references in SQL are converted to Tortoise ORM ForeignKeyField definitions with proper related_name and on_delete cascade rules.

Is my SQL data sent to a server?

No, all processing happens entirely in your browser. Your SQL schema never leaves your device.

Related Tools