-
-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for relationship building models in SQLAlchemyFactory
#467
Comments
I'm trying to build a factory for each model, building from the bottom up in the order in which the models are referenced. |
SQLAlchemyFactory
SQLAlchemyFactory
I think the settings here may cover some of the desired use cases https://polyfactory.litestar.dev/reference/factories/base.html#polyfactory.factories.base.BaseFactory.__set_as_default_factory_for_type__.
will allow changing this globally. Inheriting config for dynamically created subfactories is a known issue (see #426).
I am not sure on this rule. This works for Grade but if calling TeacherFactory would generating some grades be expected? Maybe an enum/literal values would be appropriate to allow different strategy on a per factory basis, e.g. has |
Hi @adhtruong. Thanks for the reply. For your suggestion.
This would cause the program to go into infinite recursion, which is why I need to artificially disable certain field generation.Like |
In the example I gave, the only way to get the expected generated result is to call the Giving I wrote a script to apply networkx to compute the spanning tree of model relationships and then generate the factory definition code, which is my current solution. Perhaps we can find a more elegant way to generate a series of correlated models. |
This configuration is only used by the SQLAlchemyFactory currently. A configuration here makes sense if useful to have these distinct behaves in the library itself. Going back to your previous comment
would just keeping track of seen types beforehand handle this case? Note this may be a more naive solution than full graph resolution but may suffice for a lot of use cases. Here's a quick prototype of this based on an example in the docs
Edit: Add test for example with session |
The results of this prototype don't seem to be what was expected. This assert
I think should be |
@0x587 I think that difference comes from using I've extended the example to use |
The example of this extension perfectly meets my needs, thank you. I think this need commonly arises in scenarios where fake data needs to be generated for a database. Creating a subclass of such a SQLAlchemyFactory with a nice name to put into the library is necessary. Maybe we can close this issue. |
Great, thanks for checking and confirming! I would be in favour of keeping this issue open just so the workaround is documented here as I agree with you this is a probably a common issue. I'll see if this feature is part of the main library or at least document the above as a workaround. |
Summary
Suppose I have a series of cascading models like A, B, C. When I call the factory for A and enable the
__set_relationships__
parameter, the factory will build model A and have B in each A, but no C in B.just like:
A
|-B1
|- Expected C but did not
|-B2
|- Expected C but did not
I went and read the source code and tried to modify it, and found that it was because the
__set_relationships__
parameter was not set when recursively constructing the factory. When I tried to set the parameter manually it triggered a recursive error, and this feature may require a larger change to the structure of the code.I am trying to use this library for database mock fake data, if implemented this feature will be able to stage my work.
Basic Example
Here's a sample test
Drawbacks and Impact
I try add code in
https://github.com/litestar-org/polyfactory/blob/8dc8e1a4594a75ad9a16e1b6f5041b6044fc4f51/polyfactory/factories/base.py#L650C1-L650C80
like that
This only solves recursive construction in one-to-many or many-to-many relationships and is very inelegant, raising recursion errors when I try to extend it to one-to-one or many-to-one relationships.
like that
Unresolved questions
No response
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: