question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] sqlalchemy relationship fields are absent in router response

See original GitHub issue

There 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
tiangolocommented, Oct 26, 2019

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.

2reactions
pourquoicommented, May 27, 2019

same issue while waiting for the PR, i’m setting the relationship to load every time:

parent = relationship(“Parent”, back_populates=“children”, lazy=False)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found