On Sat, Mar 23, 2019 at 12:39 PM 02JanDal <
j...@dalheimer.de> wrote:
>
> Hello,
>
> I have two simple inheritance trees using joined table inheritance, see code below. In short, MembershipType inherits BaseShopItem and Membership inherits TransactionItem and there is a one-to-many relationship between BaseShopItem and TransactionItem.
>
> This works well, but now I want to be able to get the membership type for a certain membership, as well as all memberships for a membership type. Since this information already is present (through TransactionItem.shop_item_id, thus between the parent classes) I do not want to introduce another foreign key between the subclasses. What is the most elegant way to represent this as a relationship?
Are you looking just to have Membership.transaction_items with the
TransactionItem objects filtered out and vice versa ? I think you
can use relationship() directly, Membership.membership_types =
relationship("MembershipType") / MembershipType.membership =
relationship("Membership") . You just need to be careful not to
modify both levels of relationship at once so you'd probably make
these viewonly. Alternatively, just build accessor methods with
@property that iterate Membership.transaction_items and filter out all
objects that are are not a subclass of MembershipType.
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
>
http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See
http://stackoverflow.com/help/mcve for a full description.
> ---
> 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
sqlalchemy+...@googlegroups.com.
> To post to this group, send email to
sqlal...@googlegroups.com.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.