Hi everyone,
Does anyone have a good setup for one-to-one relationships that are
always symmetric and provide a common property to access the paired
object? Ie, it's always the case that:
1) (x,y) is in the relation iff (y,x) is in the relation.
2) x.peer = y
3) y.peer = x
Here's the best thing I've come up with so far. It's not perfect -
for instance, there's no way to query by partner.
class Person(object):
def _get_partner(self):
if self.marriage:
return self.marriage.get_partner_of(self)
else:
return None
partner = property(_get_partner)
class Marriage(object):
def __init__(self, a, b):
self.partners = [a, b]
def get_partner_of(self, a):
x = list(self.partners)
x.remove(a)
return x[0]
person_t = Table('person', metadata,
Column('id', Integer, primary_key=True),
Column('marriageid', Integer, ForeignKey('marriage.id')))
marriage_t = Table('marriage', metadata,
Column('id', Integer, primary_key=True))
person_m = orm.mapper(Person, person_t)
marriage_m = orm.mapper(Marriage, marriage_t,
properties={'partners': orm.relation(Person,
backref="marriage")})
Ross
--
Ross Vandegrift
[email protected]
"If the fight gets hot, the songs get hotter. If the going gets tough,
the songs get tougher."
--Woody Guthrie
signature.asc
Description: Digital signature
