Relationship from Model Data and inheritance #1464
-
First Check
Commit to Help
Example Codeclass 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 DescriptionI am trying to create a a one to many relationship through inheritance from a model class to a table class. Thank you for your help :) Operating SystemWindows Operating System DetailsNo response SQLModel Version0.08 Python Version3.8 Additional Context |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
If you can provide an MRE, I can try to help. 👀 |
Beta Was this translation helpful? Give feedback.
-
it 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() |
Beta Was this translation helpful? Give feedback.
-
This solves the problem from the SQLModel side. From FastAPI side the SurveyDTO and the list come as JSON payload from a POST request.
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 Edit: added the additional context with related PR and issue. Thank you guys! |
Beta Was this translation helpful? Give feedback.
it works