Hi there,
I was wondering if it was possible to have an attribute on a model that is
proxied through a relationship in a many-to-one case. I know for M2M there
is association_proxy, but that does not seem to work.
Example time!
class Show(Base):
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
class Season(Base):
number = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
show_id = sqlalchemy.Column(
sqlalchemy.ForeignKey('show.id'), primary_key=True)
show = sqlalchemy.orm.relationship(Show)
class Episode(Base):
show_id = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
season_number = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
__table_args__ = (
sqlalchemy.ForeignKeyConstraint(
['show_id', 'season_number'],
['season.show_id', 'season.number']), {})
season = sqlalchemy.orm.relationship(Season, lazy='joined')
This is a structure almost anyone can recognize: A TV Show has Seasons,
each of those has an episode. Season->Show is many-to-one, Episode->Season
as well. Technically Episode->Show is also many-to-one. I can easily access
the show from an episode by doing episode.season.show, I could also define
a property (or even hybrid_property) to access the show. But I was
wondering if there is a way for SQLAlchemy to "understand" it. That is, the
same way as with the assoc. proxy in M2M.
I realize it cannot be *that* simple because what if I set a show on an
episode? Where does the season-link come in? So I *have* to provide some
information, e.g. the season number to create the intermediary object. But
the normal workflow would be to create Show -> Season -> Episode anyway.
The actual need is thus fairly limited in this example and so this is more
out of curiosity. All query reading and setting needs could be easily
accomplished by briding the attribute as described. And to fetch the show
with the season would also be easy using eager loading.
However, I'd really like to know if it is possible in such a manner that
SQLAlchemy *understands* that it's having a proxy here.
Regards,
Florian
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.