On Tue, Aug 17, 2010 at 07:32:41PM -0700, Enrico wrote:
> 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.

Hmmm, that's more of a general one-to-many relation with an
association object - it doesn't enfore one-to-one-ness or reflexivity.
There is something I don't understand in there though - the node and
adj_node properties mutually overwrite one-another with backrefs.  How
does that work?


Here's what I want to be able to do:

n1 = Node("Node1")
n2 = Node("Node2")
n1.peer = n2
print n2.peer
>>> Node("Node1")
print n1.peer
>>> Node("Node2")

q = session.query(Node)
n1 = q.filter_by(peer=n2).one()

Usual backref patterns don't work because the backref would need to be
named the same thing as the forward reference.  Hence why I went with
an association object.

But since the association object has a relation to both nodes, there
not an easy way to know that n1's peer is the OTHER entry in
n1.node.  Hence why I added the property an synonym.

Ross



> 
> 
> 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.

-- 
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

Attachment: signature.asc
Description: Digital signature

Reply via email to