Tables and Columns

Each database in the real database is represented by a table class in the Python land. These table classes map virtual columns to real columns, allowing access of them in your code easily and intuitively.

Each table class subclasses an instance of the table base - a special object that stores some metadata about the current database the application is running. The function table_base() can be used to create a new table base object for subclassing.

from asyncqlio import table_base
Table = table_base()

Internally, this makes a clone of a Table object backed by the TableMeta which is then used to customize your tables.

Defining Tables

Tables are defined by making a new class inheriting from your table base object, corresponding to a table in the database.

class Server(Table, table_name="server"):
    ...

Note that table_name is passed explicitly here - this is optional. If no table name is passed a name will be automatically generated from the table name made all lowercase.

Table classes themselves are technically instances of TableMeta, and as such all the methods of a TableMeta object are available on a table object.

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

Adding Columns

Columns on tables are represented by Column objects - these objects are strictly only on the table classes and not the rows. They provide useful functions for query building and an easy way to map data from a request onto a table.

Columns are added to table objects with a simple attribute setting syntax. To add a column to a table, you only need to do this:

class Server(Table, table_name="server"):
    id = Column(Int(), primary_key=True, unique=True)

In this example, a column called id is added to the table with the type Integer, and is set to be a primary key and unique. Of course, you can name it anything and add different type; all that matters is that the object is a Column.

Warning

Auto-incrementing columns is handled specially for databases that support the SERIAL type. To autoincrement a column on Sqlite3, use a type of Integer and primary_key=True. For other databases, use Serial (or Big/Small variants) of such.

class asyncqlio.orm.schema.column.Column(type_, *, primary_key=False, nullable=False, default=<object object>, index=True, unique=False, foreign_key=None, table=None)[source]

Bases: object

Represents a column in a table in a database.

class MyTable(Table):
    id = Column(Integer, primary_key=True)

The id column will mirror the ID of records in the table when fetching, etc. and can be set on a record when storing in a table.

sess = db.get_session()
user = await sess.select(User).where(User.id == 2).first()

print(user.id)  # 2
Parameters:
  • type (Union[ColumnType, Type[ColumnType]]) – The ColumnType that represents the type of this column.
  • primary_key (bool) – Is this column the table’s Primary Key (the unique identifier that identifies each row)?
  • nullable (bool) – Can this column be NULL?
  • default (Any) – The client-side default for this column. If no value is provided when inserting, this value will automatically be added to the insert query.
  • index (bool) – Should this column be indexed?
  • unique (bool) – Is this column unique?
  • foreign_key (Optional[ForeignKey]) – The ForeignKey associated with this column.
__init__(type_, *, primary_key=False, nullable=False, default=<object object>, index=True, unique=False, foreign_key=None, table=None)[source]
Parameters:
  • type (Union[ColumnType, Type[ColumnType]]) – The ColumnType that represents the type of this column.
  • primary_key (bool) – Is this column the table’s Primary Key (the unique identifier that identifies each row)?
  • nullable (bool) – Can this column be NULL?
  • default (Any) – The client-side default for this column. If no value is provided when inserting, this value will automatically be added to the insert query.
  • index (bool) – Should this column be indexed?
  • unique (bool) – Is this column unique?
  • foreign_key (Optional[ForeignKey]) – The ForeignKey associated with this column.
name = None

The name of the column. This can be manually set, or automatically set when set on a table.

table = None

The Table instance this Column is associated with.

type = None

The ColumnType that represents the type of this column.

default = None

The default for this column.

primary_key = None

If this Column is a primary key.

nullable = None

If this Column is nullable.

indexed = None

If this Column is indexed.

unique = None

If this Column is unique.

foreign_key = None

The foreign key associated with this column.

__repr__()[source]

Return repr(self).

__hash__()[source]

Return hash(self).

__set_name__(owner, name)[source]

Called to update the table and the name of this Column.

Parameters:
  • owner – The Table this Column is on.
  • name – The str name of this table.
table_name

The name of this column’s table.

Return type:str
autoincrement

Whether this column is set to autoincrement.

Return type:bool
classmethod with_name(name, *args, **kwargs)[source]

Creates this column with a name already set.

Return type:Column
get_ddl_sql()[source]

Gets the DDL SQL for this column.

Return type:str
generate_schema(fp=None)[source]

Generates the library schema for this column.

Return type:str
__eq__(other)[source]

Return self==value.

Return type:Union[Eq, bool]
__ne__(other)[source]

Return self!=value.

Return type:Union[NEq, bool]
__lt__(other)[source]

Return self<value.

Return type:Lt
__gt__(other)[source]

Return self>value.

Return type:Gt
__le__(other)[source]

Return self<=value.

Return type:Lte
__ge__(other)[source]

Return self>=value.

Return type:Gte
eq(other)[source]

Checks if this column is equal to something else.

Note

This is the easy way to check if a column equals another column in a WHERE clause, because the default __eq__ behaviour returns a bool rather than an operator.

Return type:Eq
ne(other)[source]

Checks if this column is not equal to something else.

Note

This is the easy way to check if a column doesn’t equal another column in a WHERE clause, because the default __ne__ behaviour returns a bool rather than an operator.

Return type:NEq
asc()[source]

Returns the ascending sorter operator for this column.

Return type:AscSorter
desc()[source]

Returns the descending sorter operator for this column.

Return type:DescSorter
set(value)[source]

Sets this column in a bulk update.

Return type:ValueSetter
incr(value)[source]

Increments this column in a bulk update.

Return type:IncrementSetter
__add__(other)[source]

Magic method for incr()

decr(value)[source]

Decrements this column in a bulk update.

Return type:DecrementSetter
__sub__(other)[source]

Magic method for decr()

quoted_fullname_with_table(table)[source]

Gets the quoted fullname with a table. This is used for columns with alias tables.

Parameters:table (TableMeta) – The Table or AliasedTable to use.
Return type:str
Returns:
quoted_name

Gets the quoted name for this column.

This returns the column name in “column” format.

quoted_fullname

Gets the full quoted name for this column.

This returns the column name in “table”.”column” format.

foreign_column
Return type:Column
Returns:The foreign Column this is associated with, or None otherwise.
__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__format__()

default object formatter

__getattribute__

Return getattr(self, name).

__init_subclass__()

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

__subclasshook__()

Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).

__weakref__

list of weak references to the object (if defined)

alias_name(table=None, quoted=False)[source]

Gets the alias name for a column, given the table.

This is in the format of t_<table name>_<column_name>.

Parameters:
  • table – The Table to use to generate the alias name. This is useful for aliased tables.
  • quoted (bool) – Should the name be quoted?
Return type:

str

Returns:

A str representing the alias name.

Primary Keys

Tables can have primary keys, which uniquely identify rows in a table, and are made up of from 1 to N columns in the table. Typically keys with multiple columns are known as compound primary keys. For convenience, an object provides primary keys on table classes.

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

Primary keys will be automatically generated on a table when multiple columns are marked as primary_key in the constructor, but a PrimaryKey object can be constructed manually and set on Table.primary_key.

Column Types

All columns in both SQL and Python have a type - the column type. This defines what data they store, what operators they can use, and so on. In asyncqlio, the first parameter passed to a column is its type; this gives it extra functionality and defines how it stores data passed to it both from the user and the database.

For implementing your own types, see creating-col-types.

Column types.

exception asyncqlio.orm.schema.types.ColumnValidationError[source]

Bases: asyncqlio.exc.DatabaseException

Raised when a column fails validation.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class asyncqlio.orm.schema.types.ColumnType[source]

Bases: abc.ABC

Implements some underlying mechanisms for a Column.

The only method that is required to be implemented on children is ColumnType.sql() - which is used in CREATE TABLE declarations, etc. ColumnType.on_set(), ColumnType.on_get() and so on are not required to be implemented - the defaults will work fine.

The ColumnType is responsible for actually loading the data from the row’s internal storage and to the user code.

# we hate fun
def on_get(self, row, column):
    return "lol"

...

# row is a random row object
# load the `fun` column which has this weird type
value = row.fun
print(value)  # "lol", regardless of what was stored in the database.

Accordingly, it is also responsible for storing the data into the row’s internal storage.

def on_set(*args, **kwargs):
    return None

row.not_fun = 1
print(row.not_fun)  # None - no value was stored in the row

To actually insert a value into the row’s storage table, use ColumnType.store_value(). Correspondingly, loading a value from the row’s storage table can be achieved with ColumnType.load_value(). These functions should be used, as they are guarenteed to work across all versions.

Columns will proxy bad attribute accesses from the Column object to this type object - meaning types can implement custom operators, if applicable.

class User(Table):
    id = Column(MyWeirdType())

...

# MyWeirdType implements `.contains`
# the contains call is proxied to (MyWeirdType instance).contains("heck")
q = await sess.select(User).where(User.id.contains("heck")).first()
column

The column this type object is associated with.

sql()[source]
Return type:str
Returns:The str SQL name of this type.
schema()[source]
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

validate_set(row, value)[source]

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row (Table) – The row being set.
  • value (Any) – The value to set.
Return type:

bool

Returns:

A bool indicating if this is valid or not.

store_value(row, value)[source]

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
on_set(row, value)[source]

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

on_get(row)[source]

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
classmethod create_default()[source]

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)[source]

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
class asyncqlio.orm.schema.types.String(size=-1)[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents a VARCHAR() type.

size = None

The max size of this String.

sql()[source]
Returns:The str SQL name of this type.
schema()[source]
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

validate_set(row, value)[source]

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row – The row being set.
  • value (Any) – The value to set.
Returns:

A bool indicating if this is valid or not.

like(other)[source]

Returns a LIKE operator, checking if this column is LIKE another string.

Parameters:other (str) – The other string to check.
Return type:Like
ilike(other)[source]

Returns an ILIKE operator, checking if this column is case-insensitive LIKE another string.

Warning

This is not supported in all DB backends.

Parameters:other (str) – The other string to check.
Return type:Union[ILike, HackyILike]
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.Text[source]

Bases: asyncqlio.orm.schema.types.String

Represents a TEXT type. TEXT type columns are very similar to String type objects, except that they have no size limit.

Note

This is preferable to the String type in some databases.

Warning

This is deprecated in MSSQL.

sql()[source]
Returns:The str SQL name of this type.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
ilike(other)

Returns an ILIKE operator, checking if this column is case-insensitive LIKE another string.

Warning

This is not supported in all DB backends.

Parameters:other (str) – The other string to check.
Return type:Union[ILike, HackyILike]
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
like(other)

Returns a LIKE operator, checking if this column is LIKE another string.

Parameters:other (str) – The other string to check.
Return type:Like
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

schema()
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
validate_set(row, value)

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row – The row being set.
  • value (Any) – The value to set.
Returns:

A bool indicating if this is valid or not.

class asyncqlio.orm.schema.types.Boolean[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents a BOOL type.

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row (Table) – The row being set.
  • value (Any) – The value to set.
Returns:

A bool indicating if this is valid or not.

classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.Integer[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents an INTEGER type.

Warning

This represents a 32-bit integer (2**31-1 to -2**32)

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Checks if this int is in range for the type.

on_set(row, value)[source]

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.SmallInt[source]

Bases: asyncqlio.orm.schema.types.Integer

Represents a SMALLINT type.

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Checks if this int is in range for the type.

classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.BigInt[source]

Bases: asyncqlio.orm.schema.types.Integer

Represents a BIGINT type.

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Checks if this int is in range for the type.

classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.Serial[source]

Bases: asyncqlio.orm.schema.types.Integer

Represents a SERIAL type.

This type does not exist in SQLite; integer primary keys autoincrement already.

sql()[source]
Returns:The str SQL name of this type.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
validate_set(row, value)

Checks if this int is in range for the type.

class asyncqlio.orm.schema.types.BigSerial[source]

Bases: asyncqlio.orm.schema.types.Serial, asyncqlio.orm.schema.types.BigInt

Represents a BIGSERIAL type.

sql()[source]
Returns:The str SQL name of this type.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
validate_set(row, value)

Checks if this int is in range for the type.

class asyncqlio.orm.schema.types.SmallSerial[source]

Bases: asyncqlio.orm.schema.types.Serial, asyncqlio.orm.schema.types.SmallInt

Represents a SMALLSERIAL type.

sql()[source]
Returns:The str SQL name of this type.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
validate_set(row, value)

Checks if this int is in range for the type.

class asyncqlio.orm.schema.types.Real[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents a REAL type.

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row – The row being set.
  • value – The value to set.
Returns:

A bool indicating if this is valid or not.

classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.Timestamp[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents a TIMESTAMP type.

sql()[source]
Returns:The str SQL name of this type.
validate_set(row, value)[source]

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row – The row being set.
  • value – The value to set.
Returns:

A bool indicating if this is valid or not.

classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
on_get(row)

Called when a value is retrieved from this column.

Parameters:row (Table) – The row that is being retrieved.
Return type:Any
Returns:The value of the row’s internal storage.
on_set(row, value)

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row (Table) – The row this value is being set on.
  • value (Any) – The value being set.
Return type:

Any

schema()
Return type:str
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
class asyncqlio.orm.schema.types.Numeric(precision=10, scale=0, asdecimal=True)[source]

Bases: asyncqlio.orm.schema.types.ColumnType

Represents a NUMERIC type.

Note

Numeric type columns are similar to Real objects, except Numeric represents an explicit known value (DECIMAL, NUMERIC, DOUBLE, etc) and not a floating point type like REAL, or FLOAT. Use Real for floating points.

Parameters:
  • precision (int) – Total number of digits stored, excluding the the decimal.
  • scale (int) – Number of digits to be stored after the decimal point.
  • asdecimal (bool) – Set value as Python decimal.Decimal, floats are used when false.
sql()[source]
Returns:The str SQL name of this type.
schema()[source]
Returns:The library schema of this object.

If this is not defined, any arguments given to the type will not persist across schema generation.

on_get(row)[source]

Called when a value is retrieved from this column.

Parameters:row – The row that is being retrieved.
Returns:The value of the row’s internal storage.
on_set(row, value)[source]

Called when a value is a set on this column.

This is the default method - it will call ColumnType.validate_set() to validate the type before storing it. This is useful for simple column types.

Parameters:
  • row – The row this value is being set on.
  • value (Any) – The value being set.
classmethod create_default()

Creates the default object for this table in the event that a type is passed to a column, instead of an instance.

Return type:ColumnType
in_(*args)

Returns an IN operator, checking if a value in this column is in a tuple of items.

Parameters:args – The items to check.
Return type:In
store_value(row, value)

Stores a value in the row’s storage table.

This is for internal usage only.

Parameters:
  • row (Table) – The row to store in.
  • value (Any) – The value to store in the row.
validate_set(row, value)

Validates that the item being set is valid. This is called by the default on_set.

Parameters:
  • row (Table) – The row being set.
  • value (Any) – The value to set.
Return type:

bool

Returns:

A bool indicating if this is valid or not.

Row Objects

In asyncqlio, a row object is simply an instance of a Table. To create one, you can call the table object (much like creating a normal instance of a class):

row = User()

To provide values for the columns, you can pass keyword arguments to the constructor corresponding with the names of the columns, like so:

row = User(id=1, name="heck")

These row objects have their column values accessible via attribute access:

print(row.id)  # prints 1

Row objects also produced from queries, and act exactly the same.