I have the following two models:
class TableRecord(SQLModel, table=True): # Define parent after child
__tablename__: str = "table_record"
record_key: Optional[int] = Field(default=None, primary_key=True)
table_id: int # Assuming foreign_key="table.id" is defined elsewhere
created_by: int # = Field(foreign_key="user.id")
values: list["TableRecordValue"] = Relationship(back_populates="table_record")
class TableRecordValue(SQLModel, table=True): # Define child first
__tablename__: str = "table_record_value"
id: Optional[int] = Field(default=None, primary_key=True)
column_id: int # Assuming foreign_key="table_column.id" is defined elsewhere
value: str
updated_on: Optional[datetime] = Field(default_factory=datetime.utcnow)
updated_by: int # Assuming foreign_key="user.id" is defined elsewhere
record_key: Optional[int] = Field(foreign_key="table_record.record_key")
table_record: "TableRecord" = Relationship(back_populates="values")
I have the following endpoint:
@table_records_router.get("/table_records/{record_key}", response_model=TableRecord)
def get_table_record(record_key: int):
with Session(engine) as session:
statement = select(TableRecord).where(TableRecord.record_key == record_key)
table_record = session.exec(statement).first()
if not table_record:
raise HTTPException(status_code=404, detail=f"TableRecord with key {record_key} not found")
print(table_record.values) # Data is present here
return table_record # The returned data from the endpoint no longer includes the values (["values"] gives keyError)
Why are the values not present anymore in the returned json?