Skip to content

Eager loading with Pydantic model and SQLAlchemy? #4737

@adityar15

Description

@adityar15

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

#sqlalchemy models

class Seller(db.Base):
    __tablename__= 'sellers'
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100))
    products = relationship("Product", backref="seller")


class Product(db.Base):
    __tablename__= 'products'
    id = Column(Integer, primary_key=True, index=True)
    seller_id = Column(Integer, ForeignKey('sellers.id'))
    name = Column(String(100))
    attributes = relationship("Attribute", backref="product")


class Attribute(db.Base):
    __tablename__ = 'attributes'
    id = Column(Integer, primary_key=True, index=True)
    product_id =  Column(Integer, ForeignKey('products.id'))
    name = Column(String(255), unique=True, index=True, nullable=False)

Description

I am trying to eager load models in Fast API. Based on the example above I want to eager load seller data and go down the tree till attribute. How to eager load it?

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.65.1

Python Version

3.8.7

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions