-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Docs: Add example to use PydanticFactory
with SQLAlchemy persistence
#504
Comments
PydanticFactory
with SQLAlchemy persistence
If my understanding is correct, you want a way to convert pydantic models into sqla models? Could you show an example of what it is that you're trying to achieve? |
I would like to use |
Aah gotcha. And how is that translation from pydantic model to SQL model happening? |
For SQLAlchemy to Pydantic: https://docs.pydantic.dev/latest/concepts/models/#arbitrary-class-instances The other way around, which is what I'm looking for:
|
Hm...I'm not a 100% sure on whether we should add this to the documentation. That being said, I do see the benefit of this and agree that it may be a relatively common use case (especially for those using FastAPI like you said). So I'm kind of 50/50 on this. Anyone have any opinions on this, @litestar-org/members? |
Summary
Hi,
I use FastAPI with a 'typical' structure: Pydantic schemas are converted to SQLAlchemy models.
For tests, currently, SQLAlchemy models are created in fixtures. Obviously, as the amount of models and validations grows, this doesn't scale. Therefore, I will use per-test objects created by factories.
polyfactory looks ideal, but one thing is missing: I would like to pass a Pydantic schema (
PydanticFactory
), then convert it to an SQLAlchemy model and create it, using a custom (CRUD layer) implementation.Reason: Pydantic schemas validate business logic that should not be violated in tests (which happens by creating SQLAlchemy models with only data validation).
This looks relatively easy to implement by passing a custom handler to
__sync_persistence__
on a factory (https://polyfactory.litestar.dev/latest/usage/configuration.html#persistence-handlers). (Still need to figure out how to subclass my factories from a base class so I don't have to repeat that...)Instead of writing the implementation myself, would the maintainers be interested in adding a first-party supported example to the documentation? I could imagine this use case is quite common, as this is relevant for basically all larger FastAPI/Pydantic/SQLAlchemy projects.
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: