SQLModel "Person" with multiple attributes of the same SQLModel "Address" without a list #793
Answered
by
dangets
mariusbrinkmann
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom typing import Optional
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine
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: Address # How to specify the relationship here?
work_address: Address # How to specify the relationship here?
if __name__ == "__main__":
home_address = Address(country="US", city="Los Angeles")
work_address = Address(country="US", city="New York City")
person = Person(
home_address=home_address,
work_address=work_address
) Description
Executing the minimum working example does not work as the relationship between the to tables is missing. What's the correct approach to create models like this if I want to keep two separate attributes instead of Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.14 Python Version3.10.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
dangets
Jun 21, 2024
Replies: 1 comment
-
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
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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