please see 
http://www.sqlalchemy.org/docs/05/mappers.html#adjacency-list-relationships 
  wrt the "remote_side" option and proper configuration of the "many- 
to-one" side of a self-referential relationship.


On Feb 1, 2009, at 3:19 AM, n00b wrote:

>
> g'day,
>
> i wanted to give the adjacency pattern  a try in the context of a dog
> pedigree database and used
> http://groups.google.com/group/sqlalchemy/browse_thread/thread/d78357121da8014a/537377ff73bdede7?lnk=gst&q=family+tree#537377ff73bdede7
> as a reference.
> the requirement at hand is to be abe to go back, say, 4-5 generations
> but i don't seem to be able to change the mapper to successively yield
> parents. here's a bit of code to illustrate.
>
> persons = Table("persons", meta,
>    Column("person_id", Integer, primary_key=True),
>    Column("name", Unicode(40), nullable=False),
>    Column("mother_id", Integer, ForeignKey("persons.person_id")),
>    Column("father_id", Integer, ForeignKey("persons.person_id"))
> )
>
>
> class Person(object):pass
>
>
> mapper(Person, persons, properties={
>    'mom': relation(Person,
> primaryjoin=persons.c.mother_id==persons.c.person_id,\
>                    backref='children_m', lazy=False, join_depth=4),
>    'dad': relation(Person,
> primaryjoin=persons.c.father_id==persons.c.person_id,\
>                    backref='children_f', lazy=False, join_depth=4)
> })
>
> #meta.drop_all()
> meta.create_all()
>
> session = Session()
>
> gd_1= Person()
> gd_1.name =u'grand dad 1'
> session.add(gd_1)
> session.commit()
>
> gm_1 = Person()
> gm_1.name =u'grand ma 1'
> session.add(gm_1)
> session.commit()
>
> gd_2 = Person()
> gd_2.name =u'grand dad 2'
> session.add(gd_2)
> session.commit()
>
> gm_2 = Person()
> gm_2.name =u'grand ma 2'
> session.add(gm_2)
> session.commit()
>
> d_1 = Person()
> d_1.name =u'dad'
> d_1.mother_id = gm_1.person_id
> d_1.father_id = gd_1.person_id
> session.add(d_1)
> session.commit()
>
> m_1 = Person()
> m_1.name =u'mom'
> m_1.mother_id = gm_2.person_id
> m_1.father_id = gd_2.person_id
> session.add(m_1)
> session.commit()
>
> c = Person()
> c.name = u'child'
> c.mother_id = m_1.person_id
> c.father_id = d_1.person_id
> session.add(c)
> session.commit()
>
> r = session.query(Person).filter(Person.name==u'child').one()
> print u'name :', r.name
> print u'mother, father id :', r.mother_id, r.father_id
> print u'mother, father obj :', r.mom, r.dad
> print u'backref children _m, _f :', r.children_m, r.children_f
> print
>
> r = session.query(Person).filter(Person.dad.contains(d_1)).one()
> print u'name :', r.name
> print u'mother, father id :', r.mother_id, r.father_id
> print u'mother, father obj :', r.mom, r.dad[0].name
> print u'backref children _m, _f :', r.children_m, r.children_f[0].name
>
> session.close()
>
> as you can see, the current mapper yields the children, if any, (and
> only on the paternal side. i'd greatly appreciate insights to a) get
> the list of parents and b) to get both maternal and paternal
> references.
>
> thx
>
>
> >


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