sqlalchemy_factory

class polyfactory.factories.sqlalchemy_factory.SQLASyncPersistence[source]

Bases: SyncPersistenceProtocol[T]

__init__(session: Session) None[source]

Sync persistence handler for SQLAFactory.

save(data: T) T[source]

Persist a single instance synchronously.

Parameters:

data – A single instance to persist.

Returns:

The persisted result.

save_many(data: list[T]) list[T][source]

Persist multiple instances synchronously.

Parameters:

data – A list of instances to persist.

Returns:

The persisted result

class polyfactory.factories.sqlalchemy_factory.SQLAASyncPersistence[source]

Bases: AsyncPersistenceProtocol[T]

__init__(session: AsyncSession) None[source]

Async persistence handler for SQLAFactory.

async save(data: T) T[source]

Persist a single instance asynchronously.

Parameters:

data – A single instance to persist.

Returns:

The persisted result.

async save_many(data: list[T]) list[T][source]

Persist multiple instances asynchronously.

Parameters:

data – A list of instances to persist.

Returns:

The persisted result

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] = False

Configuration to consider relationships property as a model field or not.

__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__', '__set_primary_key__', '__set_foreign_keys__', '__set_relationships__')

Keys to be considered as config values to pass on to dynamically created factories.

classmethod get_sqlalchemy_types() dict[Any, Callable[[], Any]][source]

Get mapping of types where column type. for sqlalchemy dialect JSON type, accepted only basic types in pydict in case sqlalchemy process JSON raise serialize error.

classmethod get_provider_map() dict[Any, Callable[[], Any]][source]

Map types to callables.

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

classmethod get_model_fields() list[polyfactory.field_meta.FieldMeta][source]

Retrieve a list of fields from the factory’s model.

Returns:

A list of field MetaData instances.