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?