gitmyhub

tortoise-orm

Python ★ 5.6k updated 1d ago

Familiar asyncio ORM for python, built with relations in mind

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.

PythonasyncioSQLitePostgreSQLMySQLMSSQLOraclesetup: moderatecomplexity 3/5

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.

Where it fits