THE ORIONIS API
Build with clarity.
Explore the building blocks of an async-first Python framework. Every module, class, and method — connected, searchable, and ready to build with.
class BelongsToManyRelation(Relation[
Constructor: BelongsToManyRelation(parent, related, table, foreign_pivot_key, ...)
Many-to-many relationship backed by an intermediate pivot table.
Mirrors Eloquent's BelongsToMany, with one deliberate difference: instead of compiling a single JOIN against the pivot table (which would require the SQL compiler to alias projected columns to avoid name collisions between both tables -- a feature it does not have yet), this relationship resolves in two steps:
- Query the pivot table for the related keys linked to the parent
key(s) involved (
RawQueryBuilder, since the pivot table has no model). - Query the related model table with whereIn(related_key, ids),
reusing the regular, fully hydrated
~orionis.orm.query.builder.ModelQueryBuildermachinery.
Composite pivot keys are out of scope: like the rest of the ORM, every key involved is a single column.
| Static Method | _default |
Derive the conventional pivot table name for two model classes. |
| Method | __init__ |
Bind the relationship. |
| Async Method | _aggregate |
Constrain an aggregate query to the resolved pivot membership. |
| Async Method | _current |
Query the pivot table for the related ids currently linked. |
| Method | _extract |
Extract the related key value from a scalar or model instance. |
| Method | _invalidate |
Discard the resolved pivot rows and generated membership clause. |
| Method | _normalize |
Normalize a scalar, model, or iterable of either into id values. |
| Method | _pivot |
Build a model-less query targeting the pivot table. |
| Async Method | _prepare |
Resolve the pivot rows once and constrain the query to their ids. |
| Async Method | _resolve |
Query the pivot table for the rows linking the captured parents. |
| Method | add |
Capture the parent key used to resolve the pivot rows. |
| Method | add |
Capture every parent key involved in an eager-loaded batch. |
| Async Method | attach |
Link the parent to the given related records via the pivot table. |
| Method | clone |
Copy query clauses and pivot filters into an independent builder. |
| Async Method | delete |
Delete related records linked through the pivot table. |
| Async Method | detach |
Unlink the parent from the given related records. |
| Async Method | exists |
Report whether at least one related row is linked. |
| Async Method | first |
Execute the query and hydrate only the first linked related row. |
| Async Method | force |
Permanently delete records linked through the pivot table. |
| Async Method | get |
Execute the query and hydrate every linked related row. |
| Async Method | get |
Retrieve every related row linked to the parent instance. |
| Method | match |
Attach the matching related rows to each parent instance. |
| Async Method | sync |
Attach exactly the given records, detaching every other one. |
| Async Method | toggle |
Attach ids not currently linked, detach ids that already are. |
| Async Method | update |
Update related records linked through the pivot table. |
| Method | where |
Filter the pivot rows considered by this relationship query. |
| Class Variable | __slots__ |
Undocumented |
| Instance Variable | _eager |
Undocumented |
| Instance Variable | _foreign |
Undocumented |
| Instance Variable | _parent |
Undocumented |
| Instance Variable | _parent |
Undocumented |
| Instance Variable | _pivot |
Undocumented |
| Instance Variable | _pivot |
Undocumented |
| Instance Variable | _prepare |
Undocumented |
| Instance Variable | _prepared |
Undocumented |
| Instance Variable | _related |
Undocumented |
| Instance Variable | _related |
Undocumented |
| Instance Variable | _related |
Undocumented |
| Instance Variable | _table |
Undocumented |
Inherited from Relation:
| Class Method | no |
Build a relationship instance without its single-parent constraint. |
| Method | __await__ |
Allow await directly on a relationship without a terminal. |
| Async Method | get |
Execute the relationship query assembled for eager loading. |
| Instance Variable | _parent |
Undocumented |
Inherited from ModelQueryBuilder (via Relation):
| Method | __getattr__ |
Expose the local scopes declared by the bound model. |
| Method | _before |
Apply the global scopes and the soft delete filter once. |
| Method | _connection |
Resolve the database connection for the bound model. |
| Method | _default |
Return the column latest and oldest default to. |
| Async Method | _eager |
Resolve every pending eager-loaded relationship for a result set. |
| Method | _exists |
Return the projection used by existence probes. |
| Async Method | _fire |
Dispatch the retrieved event for freshly hydrated models. |
| Method | _prepare |
Refresh the update timestamp of a mass update payload. |
| Method | _relation |
Build the error message for an unresolvable relationship name. |
| Method | _serialize |
Apply the model casts before values reach the database. |
| Async Method | find |
Retrieve a model by its primary key. |
| Async Method | find |
Retrieve a model by primary key or raise when absent. |
| Async Method | first |
Return the first matching row or raise when none exists. |
| Method | load |
Eager load the given relationships; alias of withRelations. |
| Method | only |
Restrict the query to soft deleted rows. |
| Async Method | paginate |
Execute the query returning a length-aware page of results. |
| Async Method | pluck |
Return one column of every matching row. |
| Async Method | restore |
Restore every soft deleted row matched by the query. |
| Method | scope |
Apply a local scope by name. |
| Async Method | value |
Return a single column value of the first matching row. |
| Method | without |
Disable one global scope for this query. |
| Method | without |
Disable several global scopes, or every one of them. |
| Method | without |
Exclude soft deleted rows; the default behavior. |
| Method | with |
Eager load the given relationships alongside the query. |
| Method | with |
Include soft deleted rows in the query results. |
| Instance Variable | _connection |
Undocumented |
| Instance Variable | _eager |
Undocumented |
| Instance Variable | _meta |
Undocumented |
| Instance Variable | _model |
Undocumented |
| Instance Variable | _scopes |
Undocumented |
| Instance Variable | _trashed |
Undocumented |
| Instance Variable | _without |
Undocumented |
Inherited from QueryBuilderBase (via Relation, ModelQueryBuilder):
| Static Method | _boundary |
Validate that a range condition carries exactly two boundaries. |
| Static Method | _materialize |
Materialize an iterable of bound values into a tuple. |
| Static Method | _normalize |
Validate and normalize a comparison operator. |
| Static Method | _resolve |
Normalize a join target into a TableDefinition. |
| Method | _add |
Append a comparison between two columns of the query. |
| Method | _add |
Append an EXISTS or NOT EXISTS condition. |
| Method | _add |
Append a join expression built from either calling convention. |
| Method | _add |
Append a set-membership condition backed by values or a subquery. |
| Method | _add |
Append a raw SQL condition to a clause list. |
| Method | _add |
Append a single-column condition of the given kind. |
| Method | _add |
Parse and append a condition to a clause list. |
| Method | _nested |
Run a grouping callback and collect the conditions it declared. |
| Method | _new |
Create a sibling builder used for nested groups and subqueries. |
| Method | _paginator |
Wrap a page of results together with its pagination metadata. |
| Method | _resolve |
Normalize a subquery argument into a select plan. |
| Method | _shallow |
Build a twin of this builder still sharing its plan. |
| Method | add |
Append columns to the current projection. |
| Method | adopt |
Bind the builder to a named connection. |
| Method | adopt |
Replace the plan this builder assembles. |
| Async Method | avg |
Return the average value of a column among matching rows. |
| Async Method | count |
Count the rows matched by the query. |
| Method | cross |
Add a CROSS JOIN to the query. |
| Method | distinct |
Collapse duplicate rows from the query results. |
| Async Method | doesnt |
Report whether the query matches no rows. |
| Method | for |
Limit the query to a single page of results. |
| Method | full |
Add a FULL OUTER JOIN to the query. |
| Method | group |
Add grouping columns to the query. |
| Method | having |
Add a post-grouping condition to the query. |
| Method | having |
Add a raw SQL post-grouping condition. |
| Async Method | insert |
Insert one or many rows into the target table. |
| Method | join |
Add an INNER JOIN to the query. |
| Method | join |
Join a subquery as a derived table with an INNER JOIN. |
| Method | latest |
Order the query by a timestamp column in descending order. |
| Method | left |
Add a LEFT OUTER JOIN to the query. |
| Method | left |
Join a subquery as a derived table with a LEFT OUTER JOIN. |
| Method | limit |
Limit the number of rows returned by the query. |
| Method | lock |
Lock the selected rows against concurrent writes. |
| Async Method | max |
Return the maximum value of a column among matching rows. |
| Async Method | min |
Return the minimum value of a column among matching rows. |
| Method | offset |
Skip the given number of rows before returning results. |
| Method | oldest |
Order the query by a timestamp column in ascending order. |
| Method | order |
Add an ordering rule to the query. |
| Method | or |
Add an OR-combined post-grouping condition to the query. |
| Method | or |
Add an OR-combined filtering condition. |
| Method | or |
Add an OR-combined comparison between two columns. |
| Method | or |
Add an OR-combined EXISTS condition. |
| Method | or |
Add an OR-combined set-membership condition. |
| Method | or |
Add an OR-combined NOT EXISTS condition. |
| Method | or |
Add an OR-combined set-exclusion condition. |
| Method | or |
Add an OR-combined IS NOT NULL condition. |
| Method | or |
Add an OR-combined IS NULL condition. |
| Method | or |
Add an OR-combined raw SQL condition. |
| Method | right |
Add a RIGHT OUTER JOIN to the query. |
| Method | right |
Join a subquery as a derived table with a RIGHT OUTER JOIN. |
| Method | select |
Restrict the query projection to the given columns. |
| Method | select |
Append a raw SQL fragment to the projection. |
| Method | select |
Append a scalar subquery to the projection under an alias. |
| Method | shared |
Lock the selected rows in shared mode. |
| Method | skip |
Skip the given number of rows; alias of offset. |
| Async Method | sum |
Return the sum of a column among matching rows. |
| Method | take |
Limit the number of rows returned; alias of limit. |
| Method | to |
Return the engine-agnostic plan assembled so far. |
| Method | union |
Append another query's rows, collapsing duplicates. |
| Method | union |
Append another query's rows, keeping duplicates. |
| Method | where |
Add an AND-combined filtering condition. |
| Method | where |
Filter rows whose column value lies between two boundaries. |
| Method | where |
Compare two columns of the query against each other. |
| Method | where |
Filter rows whose column value contains the given text. |
| Method | where |
Filter rows whose column value ends with the given text. |
| Method | where |
Keep rows for which a correlated subquery returns any row. |
| Method | where |
Filter rows matching a case-insensitive SQL LIKE pattern. |
| Method | where |
Filter rows whose column value belongs to the given set. |
| Method | where |
Filter rows whose column value matches an SQL LIKE pattern. |
| Method | where |
Filter rows whose column value lies outside two boundaries. |
| Method | where |
Keep rows for which a correlated subquery returns no row. |
| Method | where |
Filter rows not matching a case-insensitive SQL LIKE pattern. |
| Method | where |
Filter rows whose column value is outside the given set. |
| Method | where |
Filter rows whose column value does not match an SQL LIKE pattern. |
| Method | where |
Filter rows whose column value is not NULL. |
| Method | where |
Filter rows whose column value is NULL. |
| Method | where |
Add an AND-combined raw SQL condition. |
| Method | where |
Filter rows whose column value matches a regular expression. |
| Method | where |
Filter rows whose column value starts with the given text. |
| Instance Variable | _plan |
Undocumented |
Model, related: type[ TRelated], table: str | None, foreign_pivot_key: str | None, related_pivot_key: str | None, parent_key: str | None, related_key: str | None):
(source)
¶
orionis.orm.relations.Relation.__init__Bind the relationship.
| Parameters | |
parent:Model | Model instance the relationship is accessed from. |
related:type of Model | Model class the relationship targets. |
table:str or None | Pivot table name; defaults to both model names in snake_case, singular, joined by "_" in alphabetical order (for instance role_user). |
foreignstr or None | Pivot column referencing the parent; defaults to snake_case(ParentClass) + "_id". |
relatedstr or None | Pivot column referencing the related row; defaults to snake_case(RelatedClass) + "_id". |
parentstr or None | Column on the parent matched against foreign_pivot_key; defaults to the parent's primary key. |
relatedstr or None | Column on the related table matched against related_pivot_key; defaults to the related model's primary key. |
| Returns | |
None | This method does not return a value. |
Constrain an aggregate query to the resolved pivot membership.
| Parameters | |
function:AggregateFunction | Aggregate operation to perform. |
column:str | Related-model column passed to the aggregate operation. |
| Returns | |
Any | Result returned by the query builder's aggregate operation. |
Query the pivot table for the related ids currently linked.
| Returns | |
list | Related ids currently linked to the parent instance. |
Extract the related key value from a scalar or model instance.
| Parameters | |
item:Any | Related id, or a model instance to read the related key from. |
| Returns | |
Any | Related id value. |
Discard the resolved pivot rows and generated membership clause.
| Returns | |
None | This method clears the cached pivot state in place. |
Normalize a scalar, model, or iterable of either into id values.
| Parameters | |
ids:Any | A related id, model instance, or iterable of either. |
| Returns | |
list | Related id values. |
Build a model-less query targeting the pivot table.
| Returns | |
RawQueryBuilder | Fresh builder bound to the pivot table and the connection the relationship runs on. |
Resolve the pivot rows once and constrain the query to their ids.
Idempotent: repeated terminal calls on the same relationship instance reuse the first resolution instead of re-querying the pivot table.
| Returns | |
bool | True when at least one related id was found. |
Query the pivot table for the rows linking the captured parents.
| Returns | |
dict | Related ids grouped by the parent key that links to them. |
Link the parent to the given related records via the pivot table.
| Parameters | |
ids:Any | A related id, model instance, iterable of either, or a mapping of id (or model) to per-row pivot attributes. |
attributes:dict or None, optional | Extra pivot column values applied to every inserted row when ids is not already a mapping. |
| Returns | |
int | Number of pivot rows inserted. |
Copy query clauses and pivot filters into an independent builder.
| Returns | |
Self | Independent relationship builder with copied query state. |
Delete related records linked through the pivot table.
| Returns | |
int | Number of linked related records deleted. |
Unlink the parent from the given related records.
| Parameters | |
ids:Any, optional | A related id, model instance, or iterable of either; None detaches every related record currently linked. |
| Returns | |
int | Number of pivot rows deleted. |
Permanently delete records linked through the pivot table.
| Returns | |
int | Number of linked related records permanently deleted. |
orionis.orm.query.ModelQueryBuilder.getExecute the query and hydrate every linked related row.
| Returns | |
Collection | Collection of hydrated related model instances. |
Retrieve every related row linked to the parent instance.
| Returns | |
Collection | Related models; empty when the parent has no key or no pivot row links it to anything. |
Filter the pivot rows considered by this relationship query.
| Parameters | |
column:str | Pivot table column name. |
*args:Any | Either the bound value, or an operator followed by a value. |
| Returns | |
BelongsToManyRelation | The same relationship, enabling fluent chaining. |