Skip to content

Relationship from Model Data and inheritance #1464

Answered by phi-friday
Franksign asked this question in Questions
Discussion options

You must be logged in to vote

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, p…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
1 reply
@YuriiMotov
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
4 participants
Converted from issue

This discussion was converted from issue #507 on August 07, 2025 13:01.