Repository navigation
Relationship attribute not in model schema #255
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Feb 28, 2022 I think it's useful,you can see the doc :https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/
Hey @carlosporta!
I am facing very similar issue right now. Did you manage to solve this? :)@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 zephyrrrrReacted by Jinzhe, quentinleport, kiddten, LindhorstWAG and Edivan TeixeiraReacted by Jinzhe and Edivan Teixeirasame issue here
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.- 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 magicSo 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)
First Check
Commit to Help
Example Code
Description
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.
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10.2
Additional Context
No response