I have a data model that includes a table related to 2 other tables. Here is
a slimmed down version of the class declarations. Note that Rvalue is
related to both Rparam and Rqmt. There is no problem populating the database
appending to the appropriate relation properties.

class Spec(Base):
    __tablename__ = 'Spec'
    SpecID = Column(Integer, primary_key=True)
    Spec_Name = Column(String)

class Rparam(Base):
    __tablename__ = 'rp'
    RparamID = Column(Integer, primary_key=True)
    Rparam_SpecID = Column(Integer, ForeignKey(Spec.SpecID))
    Rparam_Name = Column(String)
    Spec = relation(Spec, backref='Rparam')

class Rqmt(Base):
    __tablename__ = 'rq'
    RqmtID = Column(Integer, primary_key=True)
    Rqmt_SpecID = Column(Integer, ForeignKey(Spec.SpecID))
    Rqmt_Name = Column(String)
    Spec = relation(Spec, backref='Rqmt')

class Rvalue(Base):
    __tablename__ = 'rv'
    RvalueID = Column(Integer, primary_key=True)
    Rvalue_RqmtID = Column(Integer, ForeignKey(Rqmt.RqmtID))
    Rvalue_RparamID = Column(Integer, ForeignKey(Rparam.RparamID))
    Rvalue_Name = Column(String)
    Rqmt = relation(Rqmt, backref='Rvalue')
    Rparam = relation(Rparam, backref='Rvalue')



I can join the tables and query the structure with this query. Since Rvalue
has two foreign keys, SA requires that I specify the join condition. That
makes sense because I don't see how SA could determine my intent.

q = session.query(Spec.SpecID,Spec.Spec_Name,
            Rqmt.RqmtID,Rqmt.Rqmt_Name,
            Rparam.RparamID,Rparam.Rparam_Name,
            Rvalue.RvalueID,Rvalue.Rvalue_Name).join(Rqmt,Rparam,

(Rvalue,and_(Rparam.RparamID==Rvalue.Rvalue_RparamID,Rqmt.RqmtID==Rvalue.Rvalue_RqmtID))
        )


Question:
Can I express the join to table Rvalue in terms of the relation attributes
defined on the classes instead of needing a SQL construct that essentially
is restating the foreign key relationships? The documentation for
query.join() has some examples using relation attributes, but I can't figure
out the syntax when 2 foreign keys are present.

http://www.sqlalchemy.org/docs/05/reference/orm/query.html?highlight=#sqlalchemy.orm.query.Query.join

A full example can be found at   http://pastebin.com/m120d486a

-- 
Mike Conley

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to