fictive.sqlalchemy.types module

Custom SQLAlchemy column types

Classes

SET

Emulates a Python set type using an sqlalchemy.types.JSON back end

class fictive.sqlalchemy.types.SET(*args, **kwargs)[source]

Bases: sqlalchemy.sql.type_api.TypeDecorator

Emulates a Python set type using an sqlalchemy.types.JSON back end

A set object is an unordered collection of distinct objects. SQL does not natively support guaranteed distinct collections, but this constraint can be enforced by the sqlalchemy.types.TypeEngine when reading from and writing to the database, and using sqlalchemy.event hooks when assigning to an ORM attribute. E.g.:

from sqlalchemy import Column, Integer
from fictive.sqlalchemy.types import SET

class Model(DeclarativeBase):
    __tablename__ = 'model'
    id = Column(Integer, primary_key=True)
    set_column = Column(SET)

DeclarativeBase.metadata.create_all(engine)
>>> instance = Model(set_column=[1, 2, 2, 3, 4, 3])
>>> session.add(instance)
>>> session.commit()
>>> instance.set_column
{1, 2, 3, 4}
impl

alias of sqlalchemy.sql.sqltypes.JSON

process_bind_param(value, dialect)[source]

remove duplicate elements & convert to a JSON-compatible type (list)

process_literal_param(value, dialect)[source]

Receive a literal parameter value to be rendered inline within a statement.

This method is used when the compiler renders a literal value without using binds, typically within DDL such as in the “server default” of a column or an expression within a CHECK constraint.

The returned string will be rendered into the output string.

New in version 0.9.0.

process_result_value(value, dialect)[source]

convert retrieved JSON value (a list) to a set

property python_type

Return the Python type object expected to be returned by instances of this type, if known.

Basically, for those types which enforce a return type, or are known across the board to do such for all common DBAPIs (like int for example), will return that type.

If a return type is not defined, raises NotImplementedError.

Note that any type also accommodates NULL in SQL which means you can also get back None from any type in practice.