This behavior stumped me for quite a while. At the bottom of the
code, there are two ways of adding a property to a mapper. One of
them works, and the other seems to me like it should probably work,
but it doesn't. Is this a bug?
################################
from sqlalchemy import *
meta = MetaData()
T1 = Table('T1', meta,
Column('id', Integer, primary_key=True),
Column('ver', Integer, primary_key=True),
Column('otherstuff', String(100)),
)
T2 = Table('T2', meta,
Column('id', Integer, primary_key=True),
Column('ver', Integer, primary_key=True),
Column('parent_id', Integer, nullable=False),
Column('parent_ver', Integer, nullable=False),
Column('otherstuff', String(100)),
#This foreign key is implied, but not enforced in the
database:
#ForeignKeyConstraint(['parent_id', 'parent_ver'], ['T1.id',
'T1.ver']),
)
# In both tables, for any given ID, only the row with the highest
version is relevant.
# All others are history, and should be effectively invisible.
T1_maxvers = select(
[T1.c.id, func.max(T1.c.ver).label('ver')],
group_by=[T1.c.id],
)
T1_select = select(
columns = [T1],
from_obj = [T1, T1_maxvers],
whereclause = and_(T1_maxvers.c.id==T1.c.id,
T1_maxvers.c.ver==T1.c.ver),
).alias('T1_select')
T2_maxvers = select(
[T2.c.id, func.max(T2.c.ver).label('ver')],
group_by=[T2.c.id],
)
T2_select = select(
columns = [T2],
from_obj = [T2, T2_maxvers],
whereclause = and_(T2_maxvers.c.id==T2.c.id,
T2_maxvers.c.ver==T2.c.ver),
).alias('T2_select')
class One(object): pass
class Two(object): pass
m1 = mapper(One, T1_select)
m2 = mapper(Two, T2_select)
if 0:
# This works fine:
pjoin = (T1_select.c.id==T2_select.c.parent_id) &
(T1_select.c.ver==T2_select.c.parent_ver)
fks = [T1_select.c.id, T1_select.c.ver]
else:
# But this does not:
pjoin = (T1.c.id==T2.c.parent_id) & (T1.c.ver==T2.c.parent_ver)
fks = [T1.c.id, T1.c.ver]
m1.add_property('my_twos', relation(Two, primaryjoin=pjoin,
foreign_keys=fks))
m1.compile()
################################
If I uncomment the ForeignKeyConstraint in the table definition and
remove the extra arguments to relation(), everything works. I would
have though that by passing primaryjoin and foreign_keys to relation,
I am providing the same information that having the FK constraint on
the table does.
Let me know if I should log it.
Chris Perkins
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---