Table objects.
Functions
table_base ([name, meta]) |
Gets a new base object to use for OO-style tables. |
Classes
AliasedTable (alias_name, table) |
Represents an “aliased table”. |
PrimaryKey (*cols) |
Represents the primary key of a table. |
Table (**kwargs) |
The “base” class for all tables. |
TableMeta (tblname, tblbases, class_body[, …]) |
The metaclass for a table object. |
TableMetadata () |
The root class for table metadata. |
asyncqlio.orm.schema.table.
TableMetadata
[source]¶Bases: object
The root class for table metadata. This stores a registry of tables, and is responsible for calculating relationships etc.
meta = TableMetadata()
Table = table_base(metadata=meta)
tables
= None¶A registry of table name -> table object for this metadata.
bind
¶Return type: | DatabaseInterface |
---|---|
Returns: | The DatabaseInterface bound to this metadata. |
register_table
(tbl, *, autosetup_tables=False)[source]¶Registers a new table object.
Parameters: | |
---|---|
Return type: |
get_table
(table_name)[source]¶Gets a table from the current metadata.
Parameters: | table_name (str ) – The name of the table to get. |
---|---|
Return type: | Type [Table ] |
Returns: | A Table object. |
resolve_floating_relationships
()[source]¶Resolves any “floating” relationships - i.e any relationship/foreign keys that don’t directly reference a column object.
asyncqlio.orm.schema.table.
TableMeta
(tblname, tblbases, class_body, register=True, *args, **kwargs)[source]¶Bases: type
The metaclass for a table object. This represents the “type” of a table class.
Creates a new Table instance.
Parameters: |
|
---|
metadata
= None¶The TableMetadata
for this table.
primary_key
¶Getter: | The PrimaryKey for this table. |
---|---|
Setter: | A new PrimaryKey for this table. |
Note
A primary key will automatically be calculated from columns at define time, if any
columns have primary_key
set to True.
Return type: | PrimaryKey |
---|
mro
() → list¶return a type’s method resolution order
asyncqlio.orm.schema.table.
Table
(**kwargs)[source]¶Bases: object
The “base” class for all tables. This class is not actually directly used; instead
table_base()
should be called to get a fresh clone.
table
= None¶The actual table that this object is an instance of.
iter_relationships
()[source]¶Return type: | Generator [Relationship , None , None ] |
---|---|
Returns: | A generator that yields Relationship objects for this table. |
iter_columns
()[source]¶Return type: | Generator [Column , None , None ] |
---|---|
Returns: | A generator that yields Column objects for this table. |
iter_indexes
()[source]¶Return type: | Generator [Index , None , None ] |
---|---|
Returns: | A generator that yields Index objects for this table. |
explicit_indexes
(cls)[source]¶Return type: | Generator [Index , None , None ] |
---|---|
Returns: | A generator that yields Index objects for this table. |
Only manually added indexes are yielded from this generator; that is, it ignores primary key indexes, unique column indexes, relationship indexes, etc
get_column
(column_name)[source]¶Gets a column by name.
Parameters: | column_name (str ) – The column name to lookup.
|
---|---|
Return type: | Optional [Column ] |
Returns: | The Column associated with that name, or None if no column was found. |
get_relationship
(relationship_name)[source]¶Gets a relationship by name.
Parameters: | relationship_name – The name of the relationship to get. |
---|---|
Return type: | Optional [Relationship ] |
Returns: | The Relationship associated with that name, or None if it doesn’t exist. |
get_index
(index_name)[source]¶Gets an index by name.
Parameters: | index_name – The name of the index to get. |
---|---|
Return type: | Optional [Index ] |
Returns: | The Index associated with that name, or None if it doesn’t exist. |
primary_key
¶Gets the primary key for this row.
If this table only has one primary key column, this property will be a single value. If this table has multiple columns in a primary key, this property will be a tuple.
Return type: | Union [Any , Iterable [Any ]] |
---|
get_column_value
(column, return_default=True)[source]¶Gets the value from the specified column in this row.
Warning
This method should not be used by user code; it is for types to interface with only.
Parameters: |
---|
store_column_value
(column, value, *, track_history=True)[source]¶Updates the value of a column in this row. This will also update the history of the value, if applicable.
Warning
This method should not be used by user code; it is for types to interface with only.
Parameters: |
---|
get_relationship_instance
(relation_name)[source]¶Gets a ‘relationship instance’.
Parameters: | relation_name (str ) – The name of the relationship to load. |
---|
to_dict
(*, include_attrs=False)[source]¶Converts this row to a dict, indexed by Column.
Parameters: | include_attrs (bool ) – Should this include row_attrs? |
---|---|
Return type: | dict |
generate_schema
(fp=None)[source]¶Generates a Python class body that corresponds to the current DB schema.
Return type: | str |
---|
create
(cls, *, if_not_exists=True)[source]¶Creates a table with this schema in the database.
drop
(cls, *, cascade=False, if_exists=True)[source]¶Drops this table, or a table with the same name, from the database.
Parameters: |
---|
asyncqlio.orm.schema.table.
table_base
(name='Table', meta=None)[source]¶Gets a new base object to use for OO-style tables. This object is the parent of all tables created in the object-oriented style; it provides some key configuration to the relationship calculator and the DB object itself.
To use this object, you call this function to create the new object, and subclass it in your table classes:
Table = table_base()
class User(Table):
...
Binding the base object to the database object is essential for querying:
# ensure the table is bound to that database
db.bind_tables(Table.metadata)
# now we can do queries
sess = db.get_session()
user = await sess.select(User).where(User.id == 2).first()
Each Table object is associated with a database interface, which it uses for special querying
inside the object, such as Table.get()
.
class User(Table):
id = Column(Integer, primary_key=True)
...
db.bind_tables(Table.metadata)
# later on, in some worker code
user = await User.get(1)
Parameters: |
|
---|---|
Returns: | A new Table class that can be used for OO tables. |
asyncqlio.orm.schema.table.
AliasedTable
(alias_name, table)[source]¶Bases: object
Represents an “aliased table”. This is a transparent proxy to a TableMeta
table, and
will create the right Table objects when called.
class User(Table):
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String, nullable=False, unique=True)
password = Column(String, nullable=False)
NotUser = AliasedTable("not_user", User)
Parameters: |
---|
asyncqlio.orm.schema.table.
PrimaryKey
(*cols)[source]¶Bases: object
Represents the primary key of a table.
A primary key can be on any 1 to N columns in a table.
class Something(Table):
first_id = Column(Integer)
second_id = Column(Integer)
pkey = PrimaryKey(Something.first_id, Something.second_id)
Something.primary_key = pkey
Alternatively, the primary key can be automatically calculated by passing primary_key=True
to columns in their constructor:
class Something(Table):
id = Column(Integer, primary_key=True)
print(Something.primary_key)
table
= None¶The table this primary key is bound to.
index_name
= None¶The index name of this primary key, if any