Micahael gave me this advice:
http://groups.google.com/group/sqlalchemy/browse_thread/thread/df6e451855d13a60/386232232434ff92?lnk=gst&q=enrico#386232232434ff92
in which there are two backrefs and I think it's declarative whereas
yours is classical.


On Aug 18, 4:59 am, Ross Vandegrift <r...@kallisti.us> wrote:
> 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
> r...@kallisti.us
>
> "If the fight gets hot, the songs get hotter.  If the going gets tough,
> the songs get tougher."
>         --Woody Guthrie
>
>  signature.asc
> < 1KViewDownload

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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