-
-
Notifications
You must be signed in to change notification settings - Fork 716
Issue using from_orm stripping Relationship data from models #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I am having the same issue too |
I was having the same issue, I'm confused why you are able to add Objects with children here in the Docs: But in the Fastapi + SQLmodel example there is no example of how to solve this solution only if you want to read from the Database: I'm assuming the Advanced guide might have a solution for how you create models with children. Unless I'm misunderstanding how you should create objects? but I assume you would want an endpoint to behave like @res234's create employee function: def create_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)
E.g: class TeamBase(SQLModel):
name: str = Field(index=True)
headquarters: str
class Team(TeamBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
heroes: List["Hero"] = Relationship(back_populates="team")
class TeamCreate(TeamBase):
heros: Optional[List[Hero]] = Field(default=None)
@app.post("/teams/", response_model=TeamRead)
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
db_team = Team.from_orm(team)
session.add(db_team)
session.commit()
session.refresh(db_team)
return db_team |
@Butch78 I think we are having different issues. In your first code example, you can add objects with children because you are creating the The docs do give an example of how to read from the database with children: https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/#models-with-relationships My issues is strictly with the |
Sorry my answer wasn't very clear @res234, I would like to do the same as you and I agree the 'from_orm' Doesn't quite work right. I would like to Convert Json or TeamCreate objects into a SQLModel using 'from_orm' that can then be added to the database instead of having to manually add it. |
@Butch78 No problem! |
Facing the same challenge here... :-) |
same issue here 😢 |
I'm facing the same issue. Any updates? |
I'm here for the same reason, was debugging and found out nested objects were being stripped. This is not intuitive at all, and I came across https://github.com/Wouterkoorn/sqlalchemy-pydantic-orm, but this should be a core feature of SQLModel IMO. |
same issue here 😢 |
Yeah this bit me as well. |
First Check
Commit to Help
Example Code
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 thefrom_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:
(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
The text was updated successfully, but these errors were encountered: