explaingit

tortoise/tortoise-orm

5,569PythonAudience · developerComplexity · 3/5LicenseSetup · moderate

TLDR

Tortoise ORM is an async-native Python database library with a Django-style API, letting you define tables as Python classes and query databases without writing SQL, built for asyncio web apps.

Mindmap

mindmap
  root((repo))
    What it does
      Async database access
      Django-style API
      No raw SQL needed
      Prevents SQL injection
    Supported databases
      SQLite
      PostgreSQL
      MySQL
      MSSQL and Oracle
    Features
      Model definitions
      Relationship queries
      Migration tool
      CLI commands
    Audience
      Python web developers
      AsyncIO users
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add async database access to a FastAPI or Sanic web app using Django-style model definitions.

USE CASE 2

Define relationships between tables and query them using Python code instead of raw SQL.

USE CASE 3

Use the built-in migration CLI to update your database schema automatically as your models change.

USE CASE 4

Migrate a project to an async-compatible ORM without learning a completely new query syntax.

Tech stack

PythonasyncioSQLitePostgreSQLMySQLMSSQLOracle

Getting it running

Difficulty · moderate Time to first run · 30min

Requires an async Python framework and a running database, each database type needs its own pip install extra for the async driver.

Free to use in any project, including commercial ones, with minimal restrictions.

In plain English

Tortoise ORM is a Python library for working with databases using an async-native API that will feel familiar to anyone who has used Django. An ORM (Object-Relational Mapper) lets you define your database tables as Python classes and write queries using Python code instead of raw SQL, handling the translation between the two behind the scenes. It also helps prevent SQL injection automatically through the way queries are constructed. The key distinction for Tortoise ORM is that it is built on top of asyncio, Python's framework for writing asynchronous code. This makes it a good fit for async web frameworks and applications where multiple database operations can be happening at the same time without one blocking the other. The API is modeled after Django's ORM, so defining models, querying them, and managing relationships follows a similar pattern to what Django developers already know. Supported databases include SQLite, PostgreSQL, MySQL, Microsoft SQL Server, and Oracle. Each database has its own installation option via pip, pulling in the appropriate async driver for that database. The library includes a migration system with a command-line tool. You run commands to detect changes in your model definitions, generate migration files, and apply them to your database schema. This handles the typical workflow of updating your database as your application evolves over time. To get started, you define model classes, initialize Tortoise with a database URL and a reference to your models, and then write async functions that create, query, update, and relate records. Full documentation lives on a separate site. The project is licensed under the Apache License.

Copy-paste prompts

Prompt 1
Show me how to define a User model and a Post model with a foreign key in Tortoise ORM and query them in FastAPI.
Prompt 2
How do I connect Tortoise ORM to a PostgreSQL database in a FastAPI app and run async queries?
Prompt 3
I need to add a new column to an existing Tortoise ORM model. How do I generate and apply a migration?
Prompt 4
How does Tortoise ORM prevent SQL injection when I filter or search records?
Prompt 5
Show me how to set up a many-to-many relationship in Tortoise ORM for a tags system and query across it.
Open on GitHub → Explain another repo

← tortoise on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.