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

Enhancement: Add SQLModelFactory #160

Closed
2 of 4 tasks
mrharpo opened this issue Apr 5, 2023 · 2 comments · Fixed by #369
Closed
2 of 4 tasks

Enhancement: Add SQLModelFactory #160

mrharpo opened this issue Apr 5, 2023 · 2 comments · Fixed by #369
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@mrharpo
Copy link

mrharpo commented Apr 5, 2023

Description

Adapting the pets example from the documentation to SQLModel, does not generate sub-factories as expected when using Relationship, despite explicitly calling Use. Foreign keys are auto-generated correctly.

Passing in a pre-built sub-model to the parent factory works as expected.

URL to code causing the issue

No response

MCVE

Working

from sqlmodel import SQLModel, Field
from polyfactory import Use
from polyfactory.factories.pydantic_factory import ModelFactory
from typing import List, Optional


class Pet(SQLModel, table=True):
    id: Optional[int] = Field(primary_key=True, default=None)
    name: str = Field(index=True)
    sound: str = Field(index=True)


class Person(SQLModel):
    id: Optional[int] = Field(primary_key=True, default=None)
    name: str = Field(index=True)
    pets: List[Pet] = Field(index=True)


class PetFactory(ModelFactory[Pet]):
    __model__ = Pet
    __allow_none_optionals___ = False

    name = Use(ModelFactory.__random__.choice, ["Ralph", "Roxy"])


class PersonFactory(ModelFactory[Person]):
    __model__ = Person
    __allow_none_optionals___ = False

    pets = Use(PetFactory.batch, size=2)


if __name__ == '__main__':
    f = PersonFactory()
    print(f.build())

# => id=None name='amCvFfBbBknGOJkgMyjz' pets=[Pet(id=7021, name='Roxy', sound='QDzzuwnCzZlMTlibiesY'), Pet(id=1446, name='Ralph', sound='hmArwGgEkGqjJpuVOOBi')]

Not working

from sqlmodel import SQLModel, Field, Relationship
from polyfactory import Use
from polyfactory.factories.pydantic_factory import ModelFactory
from typing import List, Optional


class Pet(SQLModel, table=True):
    id: Optional[int] = Field(primary_key=True, default=None)
    name: str = Field(index=True)
    sound: str = Field(index=True)
    person_id: int = Field(foreign_key='person.id', default=None)
    person: 'Person' = Relationship(back_populates='pets')


class Person(SQLModel, table=True):
    id: Optional[int] = Field(primary_key=True, default=None)
    name: str = Field(index=True)
    pets: List[Pet] = Relationship(back_populates='person')


class PetFactory(ModelFactory[Pet]):
    __model__ = Pet
    __allow_none_optionals___ = False

    name = Use(ModelFactory.__random__.choice, ["Ralph", "Roxy"])


class PersonFactory(ModelFactory[Person]):
    __model__ = Person
    __allow_none_optionals___ = False

    pets = Use(PetFactory().batch, size=2)


if __name__ == '__main__':
    f = PersonFactory()
    print(f.build())
    # => id=None name='RbVNzbPaSyukYfQfOCZM'
    p = PetFactory()
    print('manual', f.build(pets=p.batch(size=2)))
    # => manual id=912 name='nxthozUfRKEbzRbHOezx' pets=[Pet(id=None, name='Ralph', sound='VURZSWbctAUgQOrCNsHt', person_id=2655, person=Person(id=912, name='nxthozUfRKEbzRbHOezx', pets=[...])), Pet(id=None, name='Ralph', sound='iBvnxluOSPHyYHPMPTwj', person_id=4501, person=Person(id=912, name='nxthozUfRKEbzRbHOezx', pets=[...]))]

Steps to reproduce

1. Create SQLModel models
2. See sub-factories working
3. Attach relationships
4. Sub-factories don't generate automatically

Screenshots

No response

Logs

No response

Starlite Version

  • polyfactory 1.0.0a0
  • sqlmodel 0.0.8

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
Fund with Polar
@mrharpo mrharpo added the bug Something isn't working label Apr 5, 2023
@Goldziher
Copy link
Contributor

Goldziher commented Apr 5, 2023

This is not a bug - you are expecting SQLModel relations to be created, but the pydantic factory doesnt know what these are - its not a pydantic entity at all, but an SQLModel specific thing. There needs to be a special SQLModel factory for this to work, which currently there isnt. It can be a feature request of course.

@Goldziher Goldziher added enhancement New feature or request and removed bug Something isn't working labels Apr 5, 2023
@Goldziher Goldziher changed the title Bug: SQLModel Relationship does not generate sub-models Support SQLModel relationships Apr 5, 2023
@Goldziher Goldziher changed the title Support SQLModel relationships Enhancement: Add SQLModelFactory May 29, 2023
@Goldziher Goldziher added help wanted Extra attention is needed good first issue Good for newcomers labels May 29, 2023
@guacs guacs self-assigned this Aug 26, 2023
@guacs guacs removed their assignment Sep 15, 2023
@guacs guacs mentioned this issue Sep 18, 2023
5 tasks
@guacs
Copy link
Member

guacs commented Sep 18, 2023

Closed by #369.

@guacs guacs closed this as completed Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants