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, *args, **kwargs)[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
session (Union[AsyncSession, async_scoped_session[AsyncSession]])
args (Any)
kwargs (Any)
- Returns:
The unique object instance.
- Return type:
Self
- classmethod as_unique_sync(session, *args, **kwargs)[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
session (Union[Session, scoped_session[Session]])
args (Any)
kwargs (Any)
- Returns:
The unique object instance.
- Return type:
Self
- classmethod unique_hash(*args, **kwargs)[source]¶
Generate a unique key based on the provided arguments.
This method should be implemented in the subclass.
- classmethod unique_filter(*args, **kwargs)[source]¶
Generate a filter condition for ensuring uniqueness.
This method should be implemented in the subclass.
- Parameters:
- Raises:
NotImplementedError – If not implemented in the subclass.
- Returns:
Filter condition to establish the uniqueness.
- Return type:
ColumnElement[bool]