Skip to content

Relationship from Model Data and inheritance #507

@Franksign

Description

@Franksign

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class SurveyDTO(SQLModel):
    responses: List[ResponsesDTO] = []
    # other fields.. 

class ResponsesDTO(SQLModel):
    code: int
    response: str

class SurveyTable(SurveyDTO, table='True'):
    id: Optional[int] = Field(default=None, primary_key=True)
    # how to manage relationship from DTO?

class ResponsesTable(ResponsesDTO, table='True'):
    id: Optional[int] = Field(default=None, primary_key=True)
    # how to manage relationship from DTO?

In FastAPI:

@app.post(endpoint_paths.SURVEY)
def post_survey(session: Session = Depends(get_session),
                           survey: SurveyDTO= Body(..., embed=True)) -> Response:
                 # save the survey

Description

I am trying to create a a one to many relationship through inheritance from a model class to a table class.
I don't understand how to create the relationship with the List[ResponsesDTO] in the table without duplicating code.
Maybe I am missing something?

Thank you for your help :)

Operating System

Windows

Operating System Details

No response

SQLModel Version

0.08

Python Version

3.8

Additional Context

Seems related to #18 and #469

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions