iancleary / tasks

Fastapi Application to track tasks into lists, with support for sections
https://github.com/iancleary/tasks/pkgs/container/tasks
Other
1 stars 1 forks source link

Add sqlalchemy.orm relationships between models #112

Open iancleary opened 1 year ago

iancleary commented 1 year ago

https://fastapi.tiangolo.com/tutorial/sql-databases/#create-the-relationships


from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=True)

    lists = relationship("ListObject", back_populates="owner")

class ListObject(Base):
    ...
    owner_id:int = Column(Integer, ForeignKey("users.id"))

    sections = relationship("SectionObject", back_populates="lists")

class SectionObject(Base):
    ...
    list_id:int = Column(Integer, ForeignKey("lists.id"))

    items = relationship("ItemObject", back_populates="section")

class ItemObject(Base):
    ...
    section_id:int = Column(Integer, ForeignKey("sections.id"))
iancleary commented 1 year ago

Added the question tag to see if I want to do this or not, so call it an idea to look into later