Repository navigation
Relationship from Model Data and inheritance #507
Copy link
Copy link
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Nov 23, 2022 If you can provide an MRE, I can try to help. 👀
Reacted by Franksignit works
from typing import List, Optional, ClassVar from sqlmodel import create_engine, Session, SQLModel, Field, Relationship engine = create_engine('sqlite://',echo=True) class SurveyDTO(SQLModel): ... # other fields.. class ResponsesDTO(SQLModel): code: int response: str class SurveyTable(SurveyDTO, table=True): __tablename__: ClassVar[str] = 'survey_table' id: Optional[int] = Field(default=None, primary_key=True) # how to manage relationship from DTO? responses: List["ResponsesTable"] = Relationship() class ResponsesTable(ResponsesDTO, table=True): __tablename__: ClassVar[str] = 'responses_table' id: Optional[int] = Field(default=None, primary_key=True) # how to manage relationship from DTO? survey_id: Optional[int] = Field(default=None, foreign_key='survey_table.id') survey: SurveyTable = Relationship() def main(): with Session(engine) as session: SQLModel.metadata.create_all(session.connection()) session.commit() survey = SurveyTable() response1 = ResponsesTable(code=1, response='test1') response2 = ResponsesTable(code=1, response='test2') response3 = ResponsesTable(code=1, response='test3') survey.responses = [response1, response2, response3] session.add(survey) session.commit() with Session(engine) as session: survey = session.get(SurveyTable, 1) print(survey) if survey is not None: print(survey.responses) if __name__ == '__main__': main()
Reacted by FranksignThis solves the problem from the SQLModel side. From FastAPI side the SurveyDTO and the list come as JSON payload from a POST request.
@app.post(endpoint_paths.SURVEY) def post_survey(session: Session = Depends(get_session), survey: SurveyDTO= Body(..., `embed=True))We could say that the object in the POST request should bea a SurveyTable but in this way I am exposing db data to the outside (and to documentation).
If I define the list in SurveyDTO and ovverride in SurveyTable I get
ValueError: The field responses has no matching SQLAlchemy type. Is there a way to map a field from DTO to a table field? Or I need to just duplicate the field in the parent.Edit: added the additional context with related PR and issue.
Thank you guys!
Reacted by Ryan Caught and Snehasish Barman- locked and limited conversation to collaborators
on Aug 7, 2025
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested
First Check
Commit to Help
Example Code
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