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:
import secrets
from polyfactory import Use
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory
from sqlmodel import Field, Relationship, SQLModel
def get_random_id() -> str:
return secrets.token_urlsafe(nbytes=16)
class Place(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")
class Item(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")
class PlaceFactory(SQLAlchemyFactory[Place]):
__set_relationships__ = True
class ItemFactory(SQLAlchemyFactory[Item]):
__set_relationships__ = True
place = Use(PlaceFactory.build)
def test_factories():
item = ItemFactory.build()
assert item.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:
Thanks! Keep up the great work.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too