r/FastAPI • u/coderarun • Jan 16 '25
Question What is the SQLModel equivalent of pydantic's model_rebuild()?
Context:
In this code with two dataclasses:
class User:reviews: List['Review'] ...
class Review:user: Optional[User] ...
UserSQLModel and ReviewSQLModel are generated programmatically via decorators. However, resolving the forward reference for reviews isn't working well.
This commit implements logic to replace List['Review'] annotation with List[ReviewSQLModel]at the time Review class is initialized. However, by now SQLModel has already parsed the annotations on User and created relationships. Which breaks sqlalchemy. I'm looking for a solution to resolve this. Potential options:
* Implement the equivalent of pydantic's model_rebuild(), so updated type annotations can be handled correctly.
* Use sqlalchemy's deferred reflection
* Use imperative mapping
Any other suggestions?
View all comments
Show parent comment
1
u/coderarun Jan 17 '25
Fixed. Updated PR passes my unit tests in a different repo. Please review.