Open 5cat opened 2 years ago
+1 for this working roughly the same way as @declared_attr
does in SQLAlchemy: https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/mixins.html#mixing-in-columns-in-inheritance-scenarios
Currently, the FK fields generate the correct DB columns, but the relationship attrs are dropped silently.
+1 - I couldn't figure out why Relationships were not working when I had declared them in a "base" class.
The reason why I put it in the Base class was that I was sure it would work the same as with Fields like written here: https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/?h=base#multiple-models-with-inheritance and https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/?h=base#multiple-models-with-inheritance
It would be awesome if Relationships could work the same.
First Check
Commit to Help
Example Code
Description
I want to create an SQLModel base class that contains a relationship which i can inherit. in the example there is a
HeroBase
which is inherited byAnimalHero
andRobotHero
. It inherits the columns correctly but with the relationships, i need to usesqlalchemy.orm.declared_attr
,sqlalchemy.orm.relationship
and# type: ignore
so the type checker doesnt get mad.then in insertion i need to first insert the teams, commit, then link the heroes via the
team_id
manually instead of doingAnimalHero(name="chickenman", species="chickens", team=peace)
, so the current way is very similar to thisWanted Solution
I just want to inherit the relationship provided by
SQLModel.Relationship
and doing the insertion this way.Wanted Code
Alternatives
Using directly
SQLAlchemy
instead ofSQLModel
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
Python 3.9.8
Additional Context
The reason for me to go to this route is to implement generic tables/associations and tried to look for examples in SQLAlchemy and tried to use SQLAlchemy-Utils generic_relationship with
Relationship(sa_relationship=generic_relationship("object_id", "object_type"))
, although the later works for insertion, it doesnt work when you try to getattr the relationship after selection.The wanted code is reasonable considering this is my second day using SQLModel and it feels intuitive to do it that way.