Welcome to Faker-SQLAlchemy Provider’s documentation!
SQLAlchemy Faker is a provider for the
Faker library that helps populate SQLAlchemy ORM
models with dummy data. Creating a new instance of a model can be as simple as calling
fake.sqlalchemy_model(SomeModel)
.
Installation
The recommend way to install SQLAlchemy Faker is with pip
:
pip install faker_sqlalchemy
Example
Say you have some model declared using SQLAlchemy’s ORM.
>>> class SomeModel(Base):
... __tablename__ = "some_model"
...
... id = Column(Integer, primary_key=True)
...
... value = Column(String)
And, you want to easily generate some data,
>>> from faker_sqlalchemy import SqlAlchemyProvider
>>>
>>> fake = Faker()
>>> fake.add_provider(SqlAlchemyProvider)
>>>
>>> instance = fake.sqlalchemy_model(SomeModel)
Use instance
as desired.
>>> print(instance.value)
RNvnAvOpyEVAoNGnVZQU
Supported Versions
Currently SQLAlchemy versions 1.3 and 1.4 are supported. Support for SQLAlchemy 2.0 will be added when it is released.
Faker versions >=8
are currently supported, though it should be noted that the testing matrix isn’t exhaustive. If
bugs come up with a particular version of faker beyond version 8.0, submit a ticket to add support.
Python versions >=3.7
are currently supported. If python 3.6 support is desired, submit a ticket to add support. Support
for Python 3.11 will be added when it is officially supported by SQLAlchemy. Currently, this is waiting on greenlet
releasing support for python 3.11.
Reference
- class faker_sqlalchemy.SqlAlchemyProvider(generator: Any)
Generates instances of models declared with SQLAlchemy’s ORM’s declarative_base.
Model instances are generated based on their column types. Generators for custom column types may be registered with
register_type_mapping()
.Methods:
sqlalchemy_model()
: Generates an instance of the given model.register_type_mapping()
: Tell providers which generator to use fortype
.
- classmethod register_type_mapping(type: TypeEngine, spec: Union[str, Callable[[Faker, Column], Column]])
Registers spec as a generator for columns of the given type.
spec may be:
a string indicating a method on the faker that may be called without arguments, or
A callable that accepts a faker generator and a SQLAlchemy callable.
Column types are looked up by exact match first, then base classes are searched using isinstance.
- Parameters
type – The column type that spec should apply to.
spec – The generator spec indicating how to generate the object.
- classmethod reset_type_mappings()
Resets type mappings back to defaults.
- sqlalchemy_column_value(column: Column) ColumnType
Creates an instance of a type specified by
column
.- Parameters
column – A SQLAlchemy
Column
- Returns
Returns a value that may be assigned to
Column
attributes.
- sqlalchemy_model(model: Type[ModelType], generate_primary_keys=False, generate_related=False, **overrides) ModelType
Create an instance of
model
based on its column specification.The columns of
model
are introspected to determine how their values are to be constructed. Neither primary keys, nor related models are generated by default. To generate the primary keys for the model, setprimary_keys
toTrue
. To generate related models defined byrelationship
, setgenerate_related
toTrue
.Currently,
primary_keys
andgenerate_related
are mutually exclusive, so primary keys will NOT be generated from related models. If this functionality is desired, then the keys will need to be reconciled manually.- Parameters
model – The model to create an instance of.
generate_related – Generate relationship models.
generate_primary_keys – Generate primary key fields.
overrides – Predetermined values to attach to the generated instance.
- Returns
Returns a new instance of
model
.