Skip to content

Establish relationship using Relationship feature in Postgres #1503

Answered by JLHasson
cabudies asked this question in Questions
Discussion options

You must be logged in to vote

I'm not sure what exactly the problem with your code is. One small issue is that you're instantiating db_unit before you've declared CreateUnit. I've used the code below to try to replicate and I was unsuccessful:

from typing import Optional, List

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


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

    units: List["CreateUnit"] = Relationship(back_populates="createunittype")


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

    unit_type_id: Optional[int] = Field(default=

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
Converted from issue

This discussion was converted from issue #141 on August 12, 2025 20:15.