I have two related models (one parent to many children). And that relationship is eager, as you can see when I select a parent and print it, it is possible to see its children too. But the problem happens when I want to cast the result from the select statement to a parent model, using for example the from_orm function, because there is no attribute named children on the parent schema. My problem is probably related to this issue: #224.
@AdamMisiak
I had a problem where relationship attributes would not appear at all. Aparently SQLAlchemy 1.4.36 (current version is 1.4.37) breaks relationships (source).
I installed version 1.4.35 and it fixed my issue, hope it fixes yours too!
Reacted by Jinzhe, kiddten, Kevin Kroupa, LindhorstWAG, Damian Ciftci, Edivan Teixeira and zephyrrrr
Reacted by Jinzhe, quentinleport, kiddten, LindhorstWAG and Edivan Teixeira
Hey there @carlosporta! You need to declare a model that explicitly includes the children for the response_model, otherwise you could end up with infinite recursion.
About the issue mentioned by @alp09, that one is reported here: #315, it was solved here: #322, it will be available in the next version, in a couple of hours, SQLModel 0.0.7.
I'm using sqlmodel to reduce mappings between sqlalchemy models/pydantic models
I can't use response_model as I'm using template responses and need to model dump to context ( aka problem 1 )
Currently what I did is:
from sqlmodel import SQLModel as _SQLModel
class SQLModel(_SQLModel):
"""Base Model Class."""
def model_dump(self, ...):
""Some magic happens here""
Also there is a method I've written for pagination that is available for table=True objects, which returns Page[SQLModel], so there is no way of mapping WhateverModel to WhateverModelWithRelations without extra magic
So as you understand that's not the ideal solution.
Also I've looked into the code and as I understand you don't add Relation fields as real fields that's why it doesn't work, but in fact there is no need to load relations by default cause it leads to the extra DB requests, so maybe it does make sense to limit recursion level ( for example, as it's done in sqlalchemy - recursion_depth )
Activity
4linuxfun commented on Mar 21, 2022
I think it's useful,you can see the doc :https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/
AdamMisiak commented on Jun 8, 2022
Hey @carlosporta!
I am facing very similar issue right now. Did you manage to solve this? :)
AverageFazeFan commented on Jun 9, 2022
@AdamMisiak
I had a problem where relationship attributes would not appear at all. Aparently SQLAlchemy 1.4.36 (current version is 1.4.37) breaks relationships (source).
I installed version 1.4.35 and it fixed my issue, hope it fixes yours too!
kiddten commented on Jun 20, 2022
same issue here
tiangolo commented on Aug 27, 2022
Hey there @carlosporta! You need to declare a model that explicitly includes the children for the response_model, otherwise you could end up with infinite recursion.
As @4linuxfun says, you can read the docs explaining it, the relevant section is here: https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/#dont-include-all-the-data
About the issue mentioned by @alp09, that one is reported here: #315, it was solved here: #322, it will be available in the next version, in a couple of hours, SQLModel 0.0.7.
koldakov commented on Oct 23, 2024
@tiangolo
Currently what I did is:
from sqlmodel import SQLModel as _SQLModel class SQLModel(_SQLModel): """Base Model Class.""" def model_dump(self, ...): ""Some magic happens here""Also there is a method I've written for pagination that is available for table=True objects, which returns Page[SQLModel], so there is no way of mapping WhateverModel to WhateverModelWithRelations without extra magic
So as you understand that's not the ideal solution.
Also I've looked into the code and as I understand you don't add Relation fields as real fields that's why it doesn't work, but in fact there is no need to load relations by default cause it leads to the extra DB requests, so maybe it does make sense to limit recursion level ( for example, as it's done in sqlalchemy - recursion_depth )