sqlalchemy_factory¶
- class polyfactory.factories.sqlalchemy_factory.SQLAlchemyBuildContext[source]¶
Bases:
BuildContext
- class polyfactory.factories.sqlalchemy_factory.SQLAlchemyConstraints[source]¶
Bases:
Constraints
- class polyfactory.factories.sqlalchemy_factory.SQLASyncPersistence[source]¶
Bases:
SyncPersistenceProtocol[T]- __init__(session: Session, persistence_method: SQLAlchemyPersistenceMethod = SQLAlchemyPersistenceMethod.COMMIT) None[source]¶
Sync persistence handler for SQLAFactory.
- class polyfactory.factories.sqlalchemy_factory.SQLAASyncPersistence[source]¶
Bases:
AsyncPersistenceProtocol[T]- __init__(session: AsyncSession, persistence_method: SQLAlchemyPersistenceMethod = SQLAlchemyPersistenceMethod.COMMIT) None[source]¶
Async persistence handler for SQLAFactory.
- class polyfactory.factories.sqlalchemy_factory.SQLAlchemyFactory[source]¶
Bases:
Generic[T],BaseFactory[T]Base factory for SQLAlchemy models.
- __is_base_factory__: bool = True¶
Flag dictating whether the factory is a ‘base’ factory. Base factories are registered globally as handlers for types. For example, the ‘DataclassFactory’, ‘TypedDictFactory’ and ‘ModelFactory’ are all base factories.
- __set_primary_key__: ClassVar[bool] = True¶
Configuration to consider primary key columns as a field or not.
- __set_foreign_keys__: ClassVar[bool] = True¶
Configuration to consider columns with foreign keys as a field or not.
- __set_relationships__: ClassVar[bool] = True¶
Configuration to consider relationships property as a model field or not.
- __set_association_proxy__: ClassVar[bool] = True¶
Configuration to consider AssociationProxy property as a model field or not.
- __persistence_method__: ClassVar[SQLAlchemyPersistenceMethod] = 'commit'¶
Configuration to use flush() or commit() for persistence.
- __config_keys__: tuple[str, ...] = ('__check_model__', '__allow_none_optionals__', '__set_as_default_factory_for_type__', '__faker__', '__random__', '__randomize_collection_length__', '__min_collection_length__', '__max_collection_length__', '__use_defaults__', '__forward_references__', '__set_primary_key__', '__set_foreign_keys__', '__set_relationships__', '__set_association_proxy__', '__persistence_method__')¶
Keys to be considered as config values to pass on to dynamically created factories.
- classmethod create_sync(**kwargs: Any) T[source]¶
Build and persists synchronously a single model instance.
- Parameters:
kwargs¶ – Any kwargs. If field_meta names are set in kwargs, their values will be used.
- Returns:
An instance of type T.
- async classmethod create_async(**kwargs: Any) T[source]¶
Build and persists asynchronously a single model instance.
- Parameters:
kwargs¶ – Any kwargs. If field_meta names are set in kwargs, their values will be used.
- Returns:
An instance of type T.
- classmethod get_sqlalchemy_types() dict[Any, Callable[[], Any]][source]¶
Get mapping of types where column type should be used directly.
For sqlalchemy dialect JSON type, accepted only basic types in pydict in case sqlalchemy process JSON raise serialize error.
- classmethod get_sqlalchemy_constraints() dict[type[TypeEngine], dict[str, str]][source]¶
Get mapping of SQLA type engine to attribute to constraints key.
- classmethod get_provider_map() dict[Any, Callable[[], Any]][source]¶
Map types to callables which accept no arguments and return a random value of the given type.
- Notes:
This method is distinct to allow overriding.
- Returns:
a dictionary mapping types to callables.
- classmethod is_supported_type(value: Any) TypeGuard[type[T]][source]¶
Determine whether the given value is supported by the factory.
- Parameters:
value¶ – An arbitrary value.
- Returns:
A typeguard