Hi,

My question is: Is it possible to have a many-to-many relation on a
self reference, without using an Association Object?

I'm using sqlalchemy to work with Drupal tables for a data migration
project. Drupal's taxonomy features hierarchical vocabularies, where
terms live in a term_data table and are associated with other terms
via term_hierarchy. A child can have many parents.

I've gotten the object mapping to basically work, like this:

class TermHierarchy(Base):
    __tablename__ = 'term_hierarchy'
    childid = Column(u'childid', Integer(), primary_key=True,
nullable=False)
    parentid = Column(u'parentid', Integer(), primary_key=True,
nullable=False)

class TermData(Base):
    __tablename__ = 'term_data'
    id = Column(u'id', Integer(), primary_key=True, nullable=False,
autoincrement=True)
    name = Column(u'name', String(length=255))

    children_assoc = relation(TermHierarchy,
                      primaryjoin=(TermHierarchy.parentid==id),
                      foreign_keys=[TermHierarchy.parentid],
                      backref=backref('parent', uselist=False),
                      uselist=True)

    parents_assoc = relation(TermHierarchy,
                      primaryjoin=(TermHierarchy.childid==id),
                      foreign_keys=[TermHierarchy.childid],
                      backref=backref('child', uselist=False),
                      uselist=True)

This works OK, except that to get to a parent or child I have to go
through the Association object:

>>> first = TermData('first')
>>> second = TermData('second', parent=first)
>>> third = TermData('third', parent=second)
>>> another_third = TermData('another_third', parent=second)
>>> second.children_assoc
[<selfref.TermHierarchy object at 0xa69c40c>, <selfref.TermHierarchy
object at 0xa69c5ac>]
>>> [x.child for x in second.children_assoc]
[<selfref.TermData object at 0xa69c48c>, <selfref.TermData object at
0xa69c56c>]
>>>

Since there are only 2 columns in the association table, i'd like to
leave it out of the mapping, which would simplify
running queries. I tried several variations on M:N mapping but
couldn't make it work. Before I bang my head against this wall any
more, maybe someone could tell me if it's possible?

Any advice appreciated.

Wade Leftwich
Ithaca, NY

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