Skip to content

Map attributes from link model(association table) #226

Description

@88Ocelot

Example Code

class Employee(SQLModel, table=True):
    id: uuid.UUID | None = Field(default=uuid.uuid4, primary_key=True)
    username: str
    first_name: str
    last_name: str
    email: str = Field(regex=r"^[a-zA-Z0-9_.]+@\w+\.\w+$")
    is_active: bool

    departments: List["Department"] = Relationship(
        back_populates="employees", link_model=DepartmentEmployeeLink
    )

class DepartmentEmployeeLink(SQLModel, table=True):
    department_id: int | None = Field(
        default=None, foreign_key="department.id", primary_key=True
    )
    employee_id: uuid.UUID | None = Field(
        default=None, foreign_key="employee.id", primary_key=True
    )
    primary: bool


class Department(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    employees: List["Employee"] = Relationship(
        back_populates="departments", link_model=DepartmentEmployeeLink
    )

Description

How i can map primary attribute to employees departments list?

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.10

Additional Context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions