unique

class advanced_alchemy.mixins.unique.UniqueMixin[source]

Bases: object

Mixin for instantiating objects while ensuring uniqueness on some field(s).

This is a slightly modified implementation derived from https://github.com/sqlalchemy/sqlalchemy/wiki/UniqueObject

async classmethod as_unique_async(session: AsyncSession | async_scoped_session[AsyncSession], *args: Any, **kwargs: Any) Self[source]

Instantiate and return a unique object within the provided session based on the given arguments.

If an object with the same unique identifier already exists in the session, it is returned from the cache.

Parameters:
  • session (AsyncSession | async_scoped_session[AsyncSession]) – SQLAlchemy async session

  • *args (Any) – Values used to instantiate the instance if no duplicate exists

  • **kwargs (Any) – Values used to instantiate the instance if no duplicate exists

Returns:

The unique object instance.

Return type:

Self

classmethod as_unique_sync(session: Session | scoped_session[Session], *args: Any, **kwargs: Any) Self[source]

Instantiate and return a unique object within the provided session based on the given arguments.

If an object with the same unique identifier already exists in the session, it is returned from the cache.

Parameters:
  • session (Session | scoped_session[Session]) – SQLAlchemy sync session

  • *args (Any) – Values used to instantiate the instance if no duplicate exists

  • **kwargs (Any) – Values used to instantiate the instance if no duplicate exists

Returns:

The unique object instance.

Return type:

Self

classmethod unique_hash(*args: Any, **kwargs: Any) Hashable[source]

Generate a unique key based on the provided arguments.

This method should be implemented in the subclass.

Parameters:
  • *args (Any) – Values passed to the alternate classmethod constructors

  • **kwargs (Any) – Values passed to the alternate classmethod constructors

Raises:

NotImplementedError – If not implemented in the subclass.

Returns:

Any hashable object.

Return type:

Hashable

classmethod unique_filter(*args: Any, **kwargs: Any) ColumnElement[bool][source]

Generate a filter condition for ensuring uniqueness.

This method should be implemented in the subclass.

Parameters:
  • *args (Any) – Values passed to the alternate classmethod constructors

  • **kwargs (Any) – Values passed to the alternate classmethod constructors

Raises:

NotImplementedError – If not implemented in the subclass.

Returns:

Filter condition to establish the uniqueness.

Return type:

ColumnElement[bool]