asyncqlio.orm.schema.table

Functions

table_base(name: str = , …) Gets a new base object to use for OO-style tables.

Classes

AliasedTable(alias_name: str, …) Represents an “aliased table”.
PrimaryKey(…) Represents the primary key of a table.
Table(**kwargs) The “base” class for all tables.
TableMeta(tblname: str, tblbases: tuple, …) 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 = None

The DB object bound to this metadata.

register_table(tbl: asyncqlio.orm.schema.table.TableMeta, *, autosetup_tables: bool = False) → asyncqlio.orm.schema.table.TableMeta[source]

Registers a new table object.

Parameters:
  • tbl – The table to register.
  • autosetup_tables – Should tables be setup again?
get_table(table_name: str) → typing.Type[asyncqlio.orm.schema.table.Table][source]

Gets a table from the current metadata.

Parameters:table_name – The name of the table to get.
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.

class asyncqlio.orm.schema.table.TableMeta(tblname: str, tblbases: tuple, class_body: dict, register: bool = 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 – Should this table be registered in the TableMetadata?
  • table_name – The name for this table.
columns
Returns:A list of Column this Table has.
iter_relationships() → typing.Generator[[asyncqlio.orm.schema.relationship.Relationship, NoneType], NoneType][source]
Returns:A generator that yields Relationship objects for this table.
iter_columns() → typing.Generator[[asyncqlio.orm.schema.column.Column, NoneType], NoneType][source]
Returns:A generator that yields Column objects for this table.
get_column(column_name: str, *, raise_si: bool = False) → typing.Union[asyncqlio.orm.schema.column.Column, NoneType][source]

Gets a column by name.

Parameters:column_name

The column name to lookup.

This can be one of the following:
  • The column’s name
  • The column’s alias_name() for this table
Returns:The Column associated with that name, or None if no column was found.
get_relationship(relationship_name) → typing.Union[asyncqlio.orm.schema.relationship.Relationship, NoneType][source]

Gets a relationship by name.

Parameters:relationship_name – The name of the relationship to get.
Returns:The Relationship associated with that name, or None if it doesn’t existr.
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.

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.

get_old_value(column: asyncqlio.orm.schema.column.Column)[source]

Gets the old value from the specified column in this row.

get_column_value(column: asyncqlio.orm.schema.column.Column, return_default: bool = True)[source]

Gets the value from the specified column in this row.

Parameters:
  • column – The column.
  • return_default – If this should return the column default, or NO_VALUE.
store_column_value(column: asyncqlio.orm.schema.column.Column, value: typing.Any, track_history: bool = True)[source]

Updates the value of a column in this row.

This will also update the history of the value, if applicable.

get_relationship_instance(relation_name: str)[source]

Gets a ‘relationship instance’.

Parameters:relation_name – The name of the relationship to load.
to_dict(*, include_attrs: bool = False) → dict[source]

Converts this row to a dict, indexed by Column.

Parameters:include_attrs – Should this include row_attrs?
asyncqlio.orm.schema.table.table_base(name: str = 'Table', meta: typing.Union[asyncqlio.orm.schema.table.TableMetadata, NoneType] = 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)

# 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)
# later on, in some worker code
user = await User.get(1)
Parameters:
  • name – The name of the new class to produce. By default, it is Table.
  • meta – The TableMetadata to use as metadata.
Returns:

A new Table class that can be used for OO tables.

class asyncqlio.orm.schema.table.AliasedTable(alias_name: str, table: typing.Type[asyncqlio.orm.schema.table.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 – The name of the alias for this table.
  • table – The TableMeta used to alias this table.
get_column(column_name: str) → asyncqlio.orm.schema.column.Column[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.

class asyncqlio.orm.schema.table.PrimaryKey(*cols: asyncqlio.orm.schema.column.Column)[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.