English 中文(简体)
FastAPI: returning relationship rows causes maximum recursion depth
原标题:

Here is how my tables are defined:

class User(Base):
   __tablename__ =  user 
   id: Mapped[int] = mapped_column(INTEGER, primary_key=True, init=False)
   username: Mapped[str] = mapped_column(VARCHAR, unique=True)

   purchases: Mapped[list[ Purchase ]] = relationship(back_populates=  user , init=False, cascade= all, delete , repr=False)

class Company(Base):
   __tablename__ =  company 
   id: Mapped[int] = mapped_column(INTEGER, primary_key=True, init=False)
   name: Mapped[str] = mapped_column(VARCHAR)
   
   products: Mapped[list[ Product ]] = relationship(back_populates= company , init=False, repr = False)


class Product(Base):
   __tablename__ =  product 
   id: Mapped[int] = mapped_column(INTEGER, primary_key=True, init=False)
   name: Mapped[str] = mapped_column(VARCHAR, index=True)

   company_id: Mapped[int] = mapped_column(ForeignKey( company.id ), init=False)

   company: Mapped[ Company ] = relationship(back_populates= products , repr = False)
   purchases: Mapped[list[ Purchase ]] = relationship(back_populates=  product , init=False, repr = False)


class Purchase(Base):
   __tablename__ =  purchase 
   id: Mapped[int] = mapped_column(INTEGER, primary_key=True, init=False)
   cost: Mapped[float] = mapped_column(NUMERIC(10, 2), index=True)

   user_id: Mapped[int] = mapped_column(ForeignKey( user.id ), init=False)
   product_id: Mapped[int] = mapped_column(ForeignKey( product.id ), init=False)

   user: Mapped[ User ] = relationship(back_populates= purchases , repr=False)
   product: Mapped[ Product ] = relationship(back_populates= purchases , repr=False)

This is my endpoint for getting user purchases:

@user_router.get( /get_user_purchases/{id} )
async def get_user_purchases(id: int, session: Session = Depends(get_session)):

      user = session.query(User).where(User.id == id).first()
      print(user.purchases) # works fine
      return user.purchases
      # RecursionError: maximum recursion depth exceeded while calling a Python object

Printing it to the terminal works fine, but why returning it from the endpoint results in maximum recursion depth exceeded error?

问题回答

暂无回答




相关问题
sqlalchemy does not create my foreign key

SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = as id = Column(Integer, primary_key=True) class B(Base): __tablename__ = bs id = Column(...

How to get sqlalchemy length of a string column

Consider this simple table definition (using SQLAlchemy-0.5.6) from sqlalchemy import * db = create_engine( sqlite:///tutorial.db ) db.echo = False # Try changing this to True and see what happens ...

Can SQLAlchemy update the table structure?

I am working on my first pylons + SQLAlchemy app (I m new to both). As I change my mind on the table structure, I wish there was a similar function to metadata.create_all(), that checks if there are ...

SQLAlchemy 0.5.5 - defining table schema

I used sqlautocode to generate a model.py for an existing MySQL database and here s a table example: fusion_articles = Table( fusion_articles , metadata, Column(u article_id , Integer(), ...

热门标签