더북(TheBook)

User 모델에 역 관계 정의하기

User 모델 역시 TimeSlot 모델과 마찬가지로 account 디렉터리의 models.py 파일에서 Booking 모델에 대해 1:N 관계이므로 list["Booking"]으로 자료형 각주를 합니다.

파이썬(/appserver/apps/account/models.py)

if TYPE_CHECKING:
    from appserver.apps.calendar.models import Calendar, Booking

class User(SQLModel, table=True):
    __tablename__ = "users"
    # 생략
    calendar: "Calendar" = Relationship(  
        back_populates="host",
        sa_relationship_kwargs={"uselist": False, "single_parent": True},  
    )

    bookings: list["Booking"] = Relationship(back_populates="guest")

TYPE_CHECKING에 Booking 모델을 추가한 것을 놓치지 마세요.

여기까지 진행한 커밋은 다음 URL을 참고합니다.

https://gilbut.co/c/25061491cS

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.