You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there.
After browsing the documentation (and some issues/PR), I'm still not sure if SQLModel is properly supported at this time.
SQLModel objects have the particularity to be both Pydantic and SQLAlchemy objects, so in theory, it may work using SQLAlchemyFactory.
But after trying by myself with this code:
importsecretsfrompolyfactoryimportUsefrompolyfactory.factories.sqlalchemy_factoryimportSQLAlchemyFactoryfromsqlmodelimportField, Relationship, SQLModeldefget_random_id() ->str:
returnsecrets.token_urlsafe(nbytes=16)
classPlace(SQLModel, table=True):
id: str=Field(default_factory=get_random_id, primary_key=True)
name: str=Field(unique=True)
items: list["Item"] =Relationship(back_populates="place")
classItem(SQLModel, table=True):
id: str=Field(default_factory=get_random_id, primary_key=True)
name: str=Field(unique=True)
place_id: str=Field(default=None, foreign_key="place.id")
place: Place=Relationship(back_populates="items")
classPlaceFactory(SQLAlchemyFactory[Place]):
__set_relationships__=TrueclassItemFactory(SQLAlchemyFactory[Item]):
__set_relationships__=Trueplace=Use(PlaceFactory.build)
deftest_factories():
item=ItemFactory.build()
assertitem.place_id==item.place.id# /!\ AssertionError, they are not the same
With this code, the relationship is not actually created with the factory. I guess it's because polyfactory does not detect the Relationship() field which is specific to SQLModel.
So:
Is SQLModel supported?
If not directly, is there a workaround?
Is is possible to provide some documentation about it? I guess it would valuable for people coming from fastAPI (with SQLModel) to know if they should stick to factory-boy or not, or can use a more modern approach like polyfactory.
Hi ddahan, this should work in most situations for SQLModel though may be some edges cases not supported.
The current behaviour for SQLAlchemyFactory is to not resolve foreign keys by default to avoid adding this complexity to this library itself. Using the example here and using a persistence handler so SQLA resolves these references should fix the example
Summary
Hi there.
After browsing the documentation (and some issues/PR), I'm still not sure if
SQLModel
is properly supported at this time.SQLModel objects have the particularity to be both Pydantic and SQLAlchemy objects, so in theory, it may work using
SQLAlchemyFactory
.But after trying by myself with this code:
With this code, the relationship is not actually created with the factory. I guess it's because polyfactory does not detect the
Relationship()
field which is specific to SQLModel.So:
Thanks! Keep up the great work.
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: