Skip to content
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: specify the compatibility with SQLModel more explicitely #586

Open
ddahan opened this issue Sep 19, 2024 · 1 comment
Open

Docs: specify the compatibility with SQLModel more explicitely #586

ddahan opened this issue Sep 19, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@ddahan
Copy link

ddahan commented Sep 19, 2024

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:

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:

  • 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.

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

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@ddahan ddahan added the documentation Improvements or additions to documentation label Sep 19, 2024
@adhtruong
Copy link
Collaborator

adhtruong commented Sep 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants