asyncqlio.orm.schema.types

Column types.

Classes

BigInt() Represents a BIGINT type.
BigSerial() Represents a BIGSERIAL type.
Boolean() Represents a BOOL type.
ColumnType() Implements some underlying mechanisms for a Column.
Integer() Represents an INTEGER type.
Numeric([precision, scale, asdecimal]) Represents a NUMERIC type.
Real() Represents a REAL type.
Serial() Represents a SERIAL type.
SmallInt() Represents a SMALLINT type.
SmallSerial() Represents a SMALLSERIAL type.
String([size]) Represents a VARCHAR() type.
Text() Represents a TEXT type.
Timestamp() Represents a TIMESTAMP type.

Exceptions

ColumnValidationError Raised when a column fails validation.
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.