First Check
Commit to Help
Example Code
from typing import Optional
from sqlmodel import Relationship, SQLModel, Field
class Design(SQLModel, table=True):
__tablename__: str = 'DESIGN'
DESIGN: Optional[str] = Field(default=None, primary_key=True)
project: Optional["AdvancedProject"] = Relationship(
sa_relationship_kwargs={'uselist': False}
)
class AdvancedProject(SQLModel, table=True):
__tablename__: str = 'ADVANCEDPROJECT'
PROJECT: Optional[str] = Field(default=None, primary_key=True)
MASTERDESIGN: Optional[str] = Field(default=None, foreign_key="DESIGN.DESIGN")
MASTERDESIGN_MODEL: Optional[Design] = Relationship(sa_relationship_kwargs={'uselist': False}, back_populates="project")
Description
I have 2 models -
- ADVANCEDPROJECT
- DESIGN
The ADVANCEDPROJECT has a field called MASTERDESIGN which is a foreign key to DESIGN model. I have shown in the example code the way I have coded the 2 models. Note that the DESIGN model has no back relationship to the ADVANCEDPROJECT model i.e it os only one way -> from ADVANCEDPROJECT to DESIGN. When I query for a ADVANCEDPROJECT project = db.query(AdvancedProject).filter(AdvancedProject.PROJECT == item_id).first() , I expected I would get the nested DESIGN fields too. But in turn I only see the string value of MASTERDESIGN field. Please see below the reponse:
{
"PROJECT": "0000169868185008210",
"MASTERDESIGN": "0640767955185008210",
}
But I was expecting:
{
"PROJECT": "0000169868185008210",
"MASTERDESIGN": {
"DESIGN": "0640767955185008210"
}
}
Can someone please point out my mistake / learning? Thanks in advance.
Operating System
Windows
Operating System Details
Windows Server 2019
SQLModel Version
0.0.6
Python Version
3.10.2
Additional Context
No response
First Check
Commit to Help
Example Code
Description
I have 2 models -
The ADVANCEDPROJECT has a field called MASTERDESIGN which is a foreign key to DESIGN model. I have shown in the example code the way I have coded the 2 models. Note that the DESIGN model has no back relationship to the ADVANCEDPROJECT model i.e it os only one way -> from ADVANCEDPROJECT to DESIGN. When I query for a ADVANCEDPROJECT
project = db.query(AdvancedProject).filter(AdvancedProject.PROJECT == item_id).first(), I expected I would get the nested DESIGN fields too. But in turn I only see the string value of MASTERDESIGN field. Please see below the reponse:{ "PROJECT": "0000169868185008210", "MASTERDESIGN": "0640767955185008210", }But I was expecting:
{ "PROJECT": "0000169868185008210", "MASTERDESIGN": { "DESIGN": "0640767955185008210" } }Can someone please point out my mistake / learning? Thanks in advance.
Operating System
Windows
Operating System Details
Windows Server 2019
SQLModel Version
0.0.6
Python Version
3.10.2
Additional Context
No response