First Check
Commit to Help
Example Code
fromsqlmodelimportSQLModel, Relationship, Field, create_engine, SessionfromtypingimportList, Optional, Literalengine=create_engine("sqlite://", connect_args={"check_same_thread": False})
defcreate_db_and_tables():
SQLModel.metadata.create_all(engine)
classLocationBase(SQLModel):
street: strcity: strcountry: strclassLocation(SQLModel, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
employee_id: Optional[int] =Field(default=None, foreign_key="employee.id")
employee: "Employee"=Relationship(back_populates='locations')
classEmployeeBase(SQLModel):
name: strjob: strclassEmployee(EmployeeBase, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
locations: List[Location] =Relationship(back_populates="employee")
classEmployeeCreate(EmployeeBase):
locations: List[Location]
defcreate_employees():
locations= [Location(street='State St.', city='New York', country='USA')]
e=EmployeeCreate(name='Test Employee', job='Test Job', locations=locations)
employee=Employee.from_orm(e) # this strips the locations list from the Employee objectprint(employee)
# You have to manually readd the locations before sending data to the DB.# If you skip this line then no location data will be added to the DB.e.locations=locations# Now you can commit.defmain():
create_db_and_tables()
create_employees()
if__name__=="__main__":
main()Description
Apologies if this seems trivial! When using SQLModel's from_orm method the relationship data is stripped from the outputted model. I think that there may be an issue with how the from_orm method works in SQLModel but I can't diagnose where the data is actually stripped.
I found a similar issue here: pydantic/pydantic#1334. The difference is that typically (before SQLModel) most models refer to other Pydantic models instead of other relationships. This could be an example of a typical model relationship in Pydantic:
from pydantic import BaseModel
class Employee(BaseModel):
id: int
name: str
type: str
class Address(BaseModel):
id: int
city: str
employee: Employee
class Config:
orm_mode = True
e = Employee(id=1, name='Test', type='pydantic')
a= Address(id=1, city='New York', employee=e)
print(a.from_orm(a))
(This correctly outputs id=1 city='New York' employee=Employee(id=1, name='Test', type='pydantic'))
It's not a big deal to readd the models but it becomes tedious if you have many models to manage. I was wondering if anyone else had this issue and I couldn't find it referenced anywhere else. Thank you so much!
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.8.1
Additional Context
No response
First Check
Commit to Help
Example Code
Description
Apologies if this seems trivial! When using SQLModel's
from_ormmethod the relationship data is stripped from the outputted model. I think that there may be an issue with how thefrom_ormmethod works in SQLModel but I can't diagnose where the data is actually stripped.I found a similar issue here: pydantic/pydantic#1334. The difference is that typically (before SQLModel) most models refer to other Pydantic models instead of other relationships. This could be an example of a typical model relationship in Pydantic:
(This correctly outputs
id=1 city='New York' employee=Employee(id=1, name='Test', type='pydantic'))It's not a big deal to readd the models but it becomes tedious if you have many models to manage. I was wondering if anyone else had this issue and I couldn't find it referenced anywhere else. Thank you so much!
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.8.1
Additional Context
No response