back_populates on relationship x refers to attribute y that is not a relationship #932
-
First Check
Commit to Help
Example Codeclass Cids(rx.Model, table=True):
cid: str = Field(primary_key=True)
company: str
tickets: list["Servers"] = Relationship(back_populates="ticket_number")
class Servers(rx.Model, table=True):
cid: str = Field(foreign_key="cids.cid")
ticket_number: str
instance: int
owner: str
template: str
power_state: int = 0
note: str | None = None
company: Cids = Relationship(back_populates="company") DescriptionI'm doing this in reflex which incorporates SQLModel, but there are slight differences (eg. rx.Model in class declarations) but they shouldn't be relevant to the issue. I just can't quite wrap my head around declaring relationships and all the docs and examples I've looked at haven't made it click. I've got 2 tables: one for Company IDs (Cids) and one for Servers. The Cids table is just a list of IDs with the actual company name. The Servers list contains the Company ID but I want to display the company name in the app, so I've got to do a join. Each server can only have a single company, but each company can have effectively unlimited servers. Presently when I try to start my app with the declarations above, I get:
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.16 Python Version3.11.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey, back_populates refers to the name of the relationship in the related class. So in your case you should have something like that: class Cids(rx.Model, table=True):
cid: str = Field(primary_key=True)
company: str
tickets: list["Servers"] = Relationship(back_populates="company")
class Servers(rx.Model, table=True):
cid: str = Field(foreign_key="cids.cid")
ticket_number: str
instance: int
owner: str
template: str
power_state: int = 0
note: str | None = None
company: Cids = Relationship(back_populates="tickets") |
Beta Was this translation helpful? Give feedback.
Hey, back_populates refers to the name of the relationship in the related class.
So in your case you should have something like that: