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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/d08cef0c-0a4b-47ef-b473-626a152fe8e6%40googlegroups.com.

Reply via email to