litestar-org / litestar-org/polyfactory

Docs: specify the compatibility with SQLModel more explicitely

Open
#586 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### 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:

```python
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided SQLModel example with SQLAlchemyFactory and inspect how relationship fields are handled. Document whether SQLModel is supported, any workaround identified by the investigation, and the expected relationship behavior when the documentation update is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.