Skip to content
Discussion options

You must be logged in to vote

I would be interested if there is a "more native" way to do this, but I got it to work by using sql alchemy primaryjoin

from typing import Optional
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select

class Address(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    country: str
    city: str

class Person(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)

    home_address_id: int = Field(foreign_key="address.id")
    home_address: Address = Relationship(sa_relationship_kwargs={"primaryjoin": lambda: Person.home_address_id == Address.id})

    work_address_id: int = Field(foreign_key="ad…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants