Relationship objects.
Classes
BaseLoadedRelationship (rel, row, session) |
Provides some common methods for specific relationship type subclasses. |
ForeignKey (foreign_column) |
Represents a foreign key object in a column. |
JoinLoadedOTMRelationship (rel, row, session) |
Represents a join-loaded one to many relationship. |
JoinLoadedOTORelationship (rel, row, session) |
Represents a joined one<-to->one relationship. |
Relationship (left, right, *[, load, …]) |
Represents a relationship to another table object. |
SelectLoadedRelationship (rel, row, session) |
A relationship object that uses a separate SELECT statement to load follow-on tables. |
asyncqlio.orm.schema.relationship.
ForeignKey
(foreign_column)[source]¶Bases: object
Represents a foreign key object in a column. This allows linking multiple tables together relationally.
class Server(Table):
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
owner_id = Column(Integer, foreign_key=ForeignKey("User.id")
Parameters: | foreign_column (Union [Column , str ]) – Either a Column representing the foreign column, or a str in the format <table object name>.<column name> . |
---|
asyncqlio.orm.schema.relationship.
Relationship
(left, right, *, load='select', use_iter=True, back_ref=None, table_alias=None)[source]¶Bases: object
Represents a relationship to another table object.
This object provides an easy, object-oriented interface to a foreign key relationship between two tables, the left table and the right table. The left table is the “parent” table, and the right table is the “child” table; effectively creating a one to many/many to one relationship between the two tables.
To create a relationship, there must be a column in the child table that represents the primary key of a parent table; this is the foreign key column, and will be used to load the other table.
class User(Table):
# id is the primary key of the parent table
id = Column(Integer, auto_increment=True)
name = Column(String)
# this is the relationship joiner; it uses id as the left key, and user_id as the right
# this will create a join between the two tables
inventory = Relationship(left=id, right="InventoryItem.user_id")
class InventoryItem(Table):
id = Column(BigInteger, auto_increment=True)
# user_id is the "foreign key" - it references the column User.id
user_id = Column(Integer, foreign_key=ForeignKey(User.id)
Once created, the new relationship object can be used to iterate over the child objects, using
async for
:
user = await sess.select.from_(User).where(User.id == 1).first()
async for item in user.inventory:
...
By default, the relationship will use a SELECT query to load the items; this can be changed to
a joined query when loading any table rows, by changing the load
param.
The possible values of this param are:
select
- Emits a SELECT query to load child items.joined
- Emits a join query to load child items.
For all possible options, see Relationship Loading.
Parameters: |
|
---|
left_column
= None¶The left column for this relationship.
right_column
= None¶The right column for this relationship.
use_iter
= None¶If this relationship uses the iterable format.
owner_table
= None¶The owner table for this relationship.
back_reference
= None¶The back-reference for this relationship.
load_type
= None¶The load type for this relationship.
our_column
¶Gets the local column this relationship refers to.
asyncqlio.orm.schema.relationship.
BaseLoadedRelationship
(rel, row, session)[source]¶Bases: object
Provides some common methods for specific relationship type subclasses.
Parameters: |
|
---|
set_rows
(rows)[source]¶Sets the rows for this relationship. This is an internal method, and not to be used in user code.
add
(self, row)[source]¶Adds a row to this relationship.
Warning
This will run an immediate insert/update of this row; if the parent row for this relationship is not inserted it will run an immediate insert on the parent.
Parameters: | row (Table ) – The TableRow object to add to this relationship. |
---|
asyncqlio.orm.schema.relationship.
SelectLoadedRelationship
(rel, row, session)[source]¶Bases: asyncqlio.orm.schema.relationship.BaseLoadedRelationship
A relationship object that uses a separate SELECT statement to load follow-on tables.
Parameters: |
|
---|
query
¶Gets the query for this relationship, allowing further customization. For example, to change the order of the rows returned:
async for child in parent.children.query.order_by(Child.age):
...
Return type: | SelectQuery |
---|
add
(self, row)¶Adds a row to this relationship.
Warning
This will run an immediate insert/update of this row; if the parent row for this relationship is not inserted it will run an immediate insert on the parent.
Parameters: | row (Table ) – The TableRow object to add to this relationship. |
---|
remove
(self, row)¶Removes a row from this query.
Warning
This will run an immediate UPDATE of this row to remove the foreign key.
Parameters: | row (Table ) – The TableRow object to remove from this relationship. |
---|
set_rows
(rows)¶Sets the rows for this relationship. This is an internal method, and not to be used in user code.
asyncqlio.orm.schema.relationship.
JoinLoadedOTMRelationship
(rel, row, session)[source]¶Bases: asyncqlio.orm.schema.relationship.BaseLoadedRelationship
Represents a join-loaded one to many relationship.
Parameters: |
|
---|
set_rows
(rows)[source]¶Sets the rows for this relationship. This is an internal method, and not to be used in user code.
asyncqlio.orm.schema.relationship.
JoinLoadedOTORelationship
(rel, row, session)[source]¶Bases: asyncqlio.orm.schema.relationship.BaseLoadedRelationship
Represents a joined one<-to->one relationship.
Parameters: |
|
---|
set_rows
(rows)[source]¶Sets the rows for this relationship. This is an internal method, and not to be used in user code.
add
(row)[source]¶Adds a row to this relationship.
Warning
This will run an immediate insert/update of this row; if the parent row for this relationship is not inserted it will run an immediate insert on the parent.
Parameters: | row (Table ) – The TableRow object to add to this relationship. |
---|