User 모델과 관계 맺기
사용자는 캘린더를 하나 갖습니다. 이를 1:1 관계라고 하는데, 1:N 관계를 모델에서 정의하는 것과 거의 동일합니다. 먼저 Calendar 모델에 User 모델을 관계짓겠습니다.
파이썬(/appserver/apps/calendar/models.py)
# 생략
if TYPE_CHECKING:
from appserver.apps.account.models import User
class Calendar(SQLModel, table=True):
__tablename__ = "calendars"
# 생략
google_calendar_id: str = Field(max_length=1024, description="Google Calendar ID")
host_id: int = Field(foreign_key="users.id", unique=True) # ①
host: "User" = Relationship(back_populates="calendar")