Previous topic

asyncqlio.orm.schema

Next topic

asyncqlio.orm.schema.column

This Page

asyncqlio.orm.schema.table

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.
class 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:
  • tbl (TableMeta) – The table to register.
  • autosetup_tables (bool) – Should tables be setup again?
Return type:

TableMeta

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.
setup_tables()[source]

Sets up the tables for usage in the ORM.

resolve_aliases()[source]

Resolves all alias tables on relationship objects.

resolve_backrefs()[source]

Resolves back-references.

resolve_floating_relationships()[source]

Resolves any “floating” relationships - i.e any relationship/foreign keys that don’t directly reference a column object.

generate_primary_key_indexes()[source]

Generates an index for the primary key of each table, if the dialect creates one.

New in version 0.2.0.

generate_unique_column_indexes()[source]

Generates an index for columns marked as unique in each table, if the dialect creates them.

New in version 0.2.0.

class 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:
  • register (bool) – Should this table be registered in the TableMetadata?
  • table_name – The name for this table.
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

class 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.

classmethod iter_relationships()[source]
Return type:Generator[Relationship, None, None]
Returns:A generator that yields Relationship objects for this table.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod iter_columns()[source]
Return type:Generator[Column, None, None]
Returns:A generator that yields Column objects for this table.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod iter_indexes()[source]
Return type:Generator[Index, None, None]
Returns:A generator that yields Index objects for this table.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod 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

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod get_column(column_name)[source]

Gets a column by name.

Parameters:column_name (str) –

The column name to lookup.

This can be one of the following:
  • The column’s name
  • The column’s alias_name() for this table
Return type:Optional[Column]
Returns:The Column associated with that name, or None if no column was found.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod 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.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

classmethod 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.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

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:
  • column (Column) – The column.
  • return_default (bool) – If this should return the column default, or NO_VALUE.
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:
  • column (Column) – The column to store.
  • value (Any) – The value to store in the column.
  • track_history (bool) – Should history be tracked? Only false if creating a row from a data source.
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
classmethod generate_schema(fp=None)[source]

Generates a Python class body that corresponds to the current DB schema.

Return type:str
classmethod create(cls, *, if_not_exists=True)[source]

Creates a table with this schema in the database.

classmethod drop(cls, *, cascade=False, if_exists=True)[source]

Drops this table, or a table with the same name, from the database.

Parameters:
  • cascade (bool) – If this drop should cascade.
  • if_exists (bool) – If we should only attempt to drop tables that exist.
classmethod get(cls, *conditions)[source]

Gets a single row of this table from the database.

Warning

The resulting row will not be bound to a session.

Parameters:conditions – The conditions to filter on.
Return type:Table
Returns:A new Table instance that was found in the database.
coroutine truncate(*, cascade=False)[source]

Truncates this table.

Parameters:cascade (bool) – If this truncation should cascade to other tables.

Changed in version 0.2.0: Moved from TableMeta to Table as a classmethod.

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.

class 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:
  • alias_name (str) – The name of the alias for this table.
  • table (Type[Table]) – The TableMeta used to alias this table.
get_column(column_name)[source]

Gets a column by name from the specified table.

This will use the base TableMeta.get_column(), and then search for columns via their alias name using this table.

Return type:Column
class 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)
columns = None

A list of Column that this primary key encompasses.

table = None

The table this primary key is bound to.

index_name = None

The index name of this primary key, if any