[BUG] sqlalchemy relationship fields are absent in router response
See original GitHub issueThere is still an issue with sqlalchemy Data Models similar to the https://github.com/tiangolo/fastapi/issues/4
If one has such models relationship as:
Base = declarative_base()
class Parent(Base):
children = relationship("Child", back_populates="parent")
class Child(Base):
parent_id = Column(
Integer,
ForeignKey('parent.id', onupdate='CASCADE', ondelete='CASCADE'),
index=True
)
parent = relationship("Parent", back_populates="children")
then the JSON response body of the API call does return only parent_id correct value and parent field has undefined value.
However when one uses a helper method like:
def json(self):
return {
'parent_id': self.parent_id,
'parent': self.parent
}
then response bode has all proper values.
UPDATE:
the same issue occurs with all SqlAlchemy hybrid_property fields.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Working with Related Objects - SQLAlchemy 1.4 Documentation
The relationship() construct will be used to inspect the table relationships between the Table objects that are mapped to the User and Address...
Read more >Fastapi sqlalchemy pydantic relational field - Stack Overflow
Any help in solving this is much appreciated thank you! response -> 1 -> category_name field required (type=value_error.missing). post.py models
Read more >Define Relationships Between SQLAlchemy Data Models
SQLAlchemy's ORM easily defines data models with relationships such as one-to-one, one-to-many, and many-to-many relationships.
Read more >FastAPI + SQLAlchemy example - Dependency Injector
from fastapi import APIRouter, Depends, Response, status from dependency_injector.wiring import inject, Provide from .containers import Container from ...
Read more >SQL (Relational) Databases - FastAPI
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String from sqlalchemy.orm import relationship from .database import Base class User(Base): ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The latest Pydantic version includes “ORM mode”, to solve these specific use cases.
I just finished a deep integration of it into FastAPI, just released it in version
0.30.0
🎉Here are the new docs: https://fastapi.tiangolo.com/tutorial/sql-databases/
It should solve lazy-loading, hybrid-properties, dynamic attributes, relationships, and others. And it should work with all the ORMs, SQLAlchemy, Peewee, Tortoise ORM, GINO, etc.
You declare the data you want to export in Pydantic models and they take care of extracting it from your ORM models.
same issue while waiting for the PR, i’m setting the relationship to load every time:
parent = relationship(“Parent”, back_populates=“children”, lazy=False)