더북(TheBook)

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

# 생략
from sqlmodel import SQLModel, Field, Relationship, func, Column, AutoString

class User(SQLModel, table=True):
    __tablename__ = "users"
    # 생략

    oauth_accounts: list[OAuthAccount] = Relationship(back_populates="user")  # ①
class OAuthAccount(SQLModel, table=True):
    __tablename__ = "oauth_accounts"
    # 생략

    user_id: int = Field(foreign_key="users.id")
    user: User = Relationship(back_populates="oauth_accounts")  # ②

OAuthAccount 모델의 user 모델 필드에 있는 Relationshipback_populates="user"라는 인자는 OAuthAccount 모델의 모델 필드인 user를 가리킵니다. User 모델의 oauth_accounts는 OauthAccount 모델의 user 모델 필드에 있는 Relationship에 명기된 oauth_accounts고, OAuthAccount 모델의 user 모델 필드는 User 모델에 있는 oauth_accounts 모델 필드의 Relationship에 명기된 user를 가리킵니다(②).

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