On Oct 12, 2013, at 10:35 PM, Iain Duncan <[email protected]> wrote:
that error isn't from your query, it's from the mappers trying to reconcile the relationships you've set up between them (which is something that happens as soon as you want to use your mappings, like running a query). My only impression was that maybe that ForeignKey() isn't getting used due to something about the table reflection, but I can't reproduce that. A quick test is attached, works fine. Impressions/questions here are: 1. why using mapper() / Table, declarative is much much easier to use ? provides a lot more configurational advantages. 1a. also I see usage of the extremely old word "relation", are you working from some very old tutorial or the 0.3 book (e.g. the oreilly book) ? 2. why do you need primaryjoin on gropuagent in the first place, the ForeignKey is all that's needed (I can take it out of my test case, works fine). |
from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
c = e.connect()
t = c.begin()
c.execute("""
create table appurls (
tierid integer,
groupagentid integer,
fooid integer,
somethingelse integer,
primary key (groupagentid, tierid, fooid)
)
""")
c.execute("""
create table groupagent (
groupagentid integer,
primary key (groupagentid)
)
""")
metadata = MetaData(c)
appurl_table = Table("appurls", metadata,
Column('tierid', Integer, ForeignKey('tier.tierid'), primary_key=True ),
Column('groupagentid', Integer, ForeignKey('groupagent.groupagentid'), primary_key=True ),
autoload=True
)
groupagent_table = Table("groupagent", metadata, autoload=True)
class AppUrl(object):
pass
class GroupAgent(object):
pass
mapper(AppUrl, appurl_table, properties={
'groupagent': relation(GroupAgent, backref='appurls',
primaryjoin=(appurl_table.c.groupagentid == groupagent_table.c.groupagentid)
)
})
mapper(GroupAgent, groupagent_table)
s = Session()
print s.query(AppUrl).join(AppUrl.groupagent)
|
signature.asc
Description: Message signed with OpenPGP using GPGMail
