tortoise-orm

Build async Python applications with Tortoise ORM models, queries, relations, and migrations.

Updated May 9, 2026
One-click install
npx skills add https://github.com/natelandau/cc-plugin --skill tortoise-orm-natelandau
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tortoise-orm
Source: https://github.com/natelandau/cc-plugin/tree/main/plugins/natelandau-toolkit/skills/tortoise-orm
Command: npx skills add https://github.com/natelandau/cc-plugin --skill tortoise-orm-natelandau

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing async database code with Tortoise ORM involves many version-specific pitfalls: lazy QuerySets that silently return unexecuted queries, unfetched relations that raise NoValuesFetched, deprecated v0.x APIs, and confusion between Python-side auto_now and database-level defaults. This Skill provides accurate, v1.x-current guidance so generated Tortoise code works correctly the first time. ## Core Features & Use Cases - Model and relation definitions: Fields, ForeignKey, ManyToMany, OneToOne relations, Meta options, inheritance via abstract mixins, and validators. - Querying and transactions: Lazy QuerySets, Q objects, F expressions, aggregation, bulk operations, select_related/prefetch_related, and atomic transaction blocks. - Full lifecycle support: FastAPI lifespan integration, Pydantic serialization, pytest-based testing with tortoise_test_context, signals, and the built-in v1.1.7 migration system replacing Aerich. - Use Case: When asked to add an async user model with related profiles to a FastAPI app, the Skill produces correct v1.x code using RegisterTortoise lifespan, awaited QuerySets, and proper relation fetching. ## Quick Start Ask the assistant to create a Tortoise ORM model with relations and async CRUD queries for your FastAPI application.

Frequently Asked Questions about tortoise-orm

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define models and relations in Tortoise ORM?

Define models by subclassing tortoise.models.Model and declaring fields like CharField or IntField. Use ForeignKeyField, ManyToManyField, and OneToOneField for relations, specifying the model string, related_name, and on_delete behavior.

How do I query and filter data with Tortoise ORM?

Tortoise QuerySets are lazy and must be awaited. Use Model.filter() with double-underscore operators like __gt or __icontains, combine conditions with Q objects, and eager-load relations with select_related or prefetch_related.

Does Tortoise ORM work with FastAPI?

Yes, Tortoise ORM integrates with FastAPI through RegisterTortoise used inside the v1.x lifespan context manager. This handles database initialization and cleanup, and pydantic_model_creator generates serialization schemas from your models.

Why does accessing a Tortoise ORM relation raise NoValuesFetched?

NoValuesFetched occurs when accessing a relation that was not explicitly fetched, since Tortoise has no lazy loading. Fix it by calling await obj.fetch_related("field") or using select_related/prefetch_related in the original query.

How do I run migrations in Tortoise ORM v1.x?

Tortoise v1.1.7 includes a built-in migration system replacing Aerich. Run tortoise init, tortoise makemigrations, and tortoise migrate from the CLI, and use RunPython or RunSQL for data migrations.

What is the difference between auto_now and db_default in Tortoise fields?

auto_now and auto_now_add set timestamps only in Python and create no database DEFAULT clause. Use db_default=Now() from tortoise.fields.db_defaults when the database itself must set the default, such as for raw SQL inserts.