Hi,

I'm looking for specific help with building an self-referential 
relationship (adjacency?) inside of inherited ORM classes. I'm also looking 
for a sanity check on the basic design of the classes since I'm not an 
expert in SQL. This in the context of a Flask-sqlalchemy app resembling a 
magazine or journal. 

Here is the logic I'd like to express, briefly:  There can be many kinds of 
Writing. Articles are writing that are associated with Issues (not shown 
here). Responses are writing that are linked to other writing, somewhat 
like a conversation or exchange.

class Writing(Base):
    # I could have all sorts of different genres
    id = Column(Integer, primary_key=True)
    # title
    # text
    # authors = many to many relation table
    type = Column(String)
    __mapper_args__ = { 
          'polymorphic_identity': 'writing',
          'polymorphic_on': type
          }   

class Article(Writing):
    id = Column(ForeignKey('writing.id'), primary_key=True)
    # inherits some stuff from Writing
    # Also defined here are columns relating to an Issue, resembling a 
    # Table of contents
    # issue_id... Each article is associated with an Issue obj
    # position... Each article has a position in the issue
    __mapper_args__ = { 
          'polymorphic_identity': 'article', }


class Response(Writing):
    # All responses are associated with Writing. Writing/Articles may have
    # several responses
    # 
    __tablename__ = 'response'
    id = Column('id', ForeignKey('writing.id'), primary_key=True)
    respondee_id = Column('respondee', ForeignKey('article.id'), 
nullable=False)
 
    # So, No, I don't know what I'm doing here...
    respondee = relationship(
        'Response', backref='respondant',
        primaryjoin=('Writing.respondant' == 'Response.respondee'))
    __mapper_args__ ={
        'polymorphic_identity': 'response', }

And my exception:

sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 
'writing' and 'response'; tables have more than one foreign key constraint 
relationship between them. Please specify the 'onclause' of this join 
explicitly.


Thank you for help and any feedback. I can throw a more detailed example on 
gist if I need to. I am hoping the answer is obvious to more experienced 
eyes.

Luke Thomas Mergner
Glendale, CA
@lmergner on twitter

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to