There is a section in the SQLAlchemy docs that deals with this: https://docs.sqlalchemy.org/en/13/orm/examples.html#module-examples.generic_associations
The first three examples in that section use different table structures so that you can use proper database-level foreign keys to maintain referential integrity. But since you've already got a db schema you want to work with, you probably need the generic_fk.py example instead. Hope that helps, Simon On Thu, Aug 29, 2019 at 8:54 PM YKdvd <[email protected]> wrote: > > Suppose I have two distinct and unrelated ORM classes, call them Powers, and > Dominions (which are in tables "powers" and "dominions", both having an "id" > field), and I want them both to have relationships to their own subclass of a > single-table polymorphic setup, call it Things. > > > class AbstractThing(...): > __tablename__ = "things" > > polyType = Column(Integer, index=True) # polymorphic key for various > flavours of things > __mapper_args__ = {'polymorphic_on': polyType, 'polymorphic_identity': -1} > # never instantiated > id = Column(Integer, primary_key=True) > > owner_id = Column(Integer) # trying to relate this to completely different > classes in the subclasses below > > class PowerThing(AbstractThing): > > __mapper_args__ = {'polymorphic_identity': 1} > > ForeignKeyConstraint(['owner_id'], ['powers.id']) > > itsPower = relationship("Powers", backref="itsThings") > > > class DominionThing(AbstractThing): > > __mapper_args__ = {'polymorphic_identity': 2} > > ForeignKeyConstraint(['owner_id'], ['dominions.id']) > > itsDominion = relationship("Dominions", backref="itsThings") > > > Is it possible (even if inadvisable) to do something like this somehow? The > relationship() lines don't find a join condition, and adding a > primaryjoin="PowerThing.owner_id==Powers.id" clause doesn't seem to do it. > > Is there some combination of foreign_keys and join conditions that would let > "owner_id" relate to different tables in different polymorphic subclasses. > I've got an existing setup like this that I'd like to map to SQLAlchemy for > now, rather than redo. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/d08cef0c-0a4b-47ef-b473-626a152fe8e6%40googlegroups.com. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeRmM4fYO9FH_ukeJMPJOjQ8s9abqy%2BQjCJjjQkuvuQnA%40mail.gmail.com.
