pypika-tortoise is an extension of the PyPika SQL query builder library, specifically designed to integrate seamlessly with Tortoise-ORM. It enables users to construct complex SQL queries using a Pythonic API directly from Tortoise-ORM models, leveraging PyPika's powerful query generation capabilities while ensuring compatibility with Tortoise-ORM's database interactions. The current version is 0.6.5, with its development closely tied to updates in `pypika` and `tortoise-orm`.
pip install pypika-tortoiseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Tortoise-ORM model and then use `pypika-tortoise` to construct SQL queries. It shows how to initialize `Table` directly from a model and build a `SELECT` query with filtering, ordering, and limiting. An example with aggregation functions is also included, showcasing how `pypika-tortoise` can generate complex SQL without needing an active database connection.
Refer to the pypika-tortoise documentation and examples, especially when migrating from a pure pypika setup or encountering unexpected behavior with advanced pypika features.
Always initialize `Table` objects with your Tortoise-ORM model class (e.g., `Table(MyModel)`) to leverage automatic column resolution and integration. Ensure fields are accessed through these model-bound `Table` instances.
Consistently use the `Field` instances provided by the model-bound `Table` object (e.g., `products_table.name`) within your `pypika-tortoise` queries.