Replies: 1 comment 1 reply
-
Hopefully this helps other people who are stuck on relationships. I did manage to get it to work but via using schemas still (will read more about it to verify if setting table to false or just not including would replace the schemas like how I'm thinking) I began by changing the file structure a bit. I now instead created a src folder under the main folder to hold each category of folders, then in each folder it has the models and schemas associated with each category. But the examples that I figured out works are adding this to the model you want the data in since I wanted to find out the user info of this row Then adding this on the many model User schema uncomment if I want it to go the other way. I currently do not want this, so commented out: Other schema, not sure if it matters but it's inheriting base and just adding the relationship properties on the other class. I did this mainly for create/update schemas back when I first created this and probably will keep it that way. Solution is probably not 100% correct either but looks like it works. I assumed initially with SQLModel conversion, I could fully remove schemas. But it looks like as of right now, no or at least not like I thought. I'm sure I could make it so that I base models off of the base SQLModel model class and not include the table, but I'll have to mess around with it more and see if I can get it working, now that I remember and understand how back_populates works as well (it just associates the sqlalchemy side to setup joins and everything, it doesn't tell the api to return it, that's what a regular model would do aka not including table) This video also helped me https://www.youtube.com/watch?v=H-7NNblkzrg |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
Migrating from 2-3 year old code that I abandoned, now bringing it back to upgrade everything to latest FastAPI, python, etc. That's when I found out about SQLModel, and figured at the same time I could migrate to that as well.
Everything that doesn't use relationship is working fine (I even managed to get fastapi_crudrouter to work after forking it since that was abandoned it looks like, so had to adjust for Pydantic changes, but overall everything I've found works. Migrated to latest on everything and even moved from MySQL to Postgres successfully.
Main issue is the relationships, I've spent countless hours, 50+ tabs, trying to understand why SQLModel won't display the properties but I can't seem to figure it out.
Here's what I've tried so far:
and on the other model side
2nd model works fine but the first model returns a circular import error
class model(base, table=true):
to_airport: "Airport" | None = relationship("Airport", foreign_keys=to_airport_id)
or to _airport: Airport | None = Relationship(sa_relationship_kwargs={"lazy": "joined",
"primaryjoin": "model.to_airport_id==airport.id"}
I'm not sure if I'm just missing something with SQLModel or what, but I think I tried just about everything I could think of. I think i kept confusing Copilot as I kept making so many changes for hours.
BTW most of my relationships are supposed to be one to many. I do have one that is many to many but I wanted to focus on the one to many ones first.
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.24
Python Version
Python 3.11.5 & just updated to Python 3.13.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions