On Feb 7, 2012, at 3:31 PM, Dave wrote:

> I've got a class Keyword that is many to many to itself through a
> mapped association table. The association table tracks which keyword
> is a parent of through the keywords ids (PK). Please see the pastebin.
> 
> http://pastebin.com/774YmjKD
> 
> I have been reading the <a href="http://docs.sqlalchemy.org/en/latest/
> orm/relationships.html#sqlalchemy.orm.relationship">many to many</a>
> and <a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/
> associationproxy.html">association proxy</a> documentation but can't
> figure out how to get sqlalchemy to do what I want.

the association proxy can be given any attribute name to point towards.  your 
example gets a little murky when you're trying to mix the usage of 
"secondary/viewonly" along with an association object, so below is a 
simplification that produces a smooth interface between Keywords:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

Base= declarative_base()

class Keyword(Base):
    __tablename__ = "keywords"
    id = Column(Integer, primary_key=True, autoincrement=True)

    parents = association_proxy('parent_relations', 'pkeyword', creator=lambda 
p:Relation(p, None))
    children = association_proxy('child_relations', 'ckeyword', creator=lambda 
c:Relation(None, c))

class Relation(Base):
    __tablename__ = "relations"

    def __init__(self, p, c):
        self.pkeyword = p
        self.ckeyword = c

    parent = Column(Integer, ForeignKey('keywords.id', ondelete="CASCADE"), 
primary_key=True)
    rcw = Column(Integer, ForeignKey('keywords.id', ondelete="CASCADE"), 
primary_key=True)

    pkeyword = relationship("Keyword", 
primaryjoin="Relation.parent==Keyword.id", 
                                    backref=backref("child_relations", 
collection_class=set))
    ckeyword = relationship("Keyword", primaryjoin="Relation.rcw==Keyword.id", 
                                    backref=backref("parent_relations", 
collection_class=set))


k1, k2, k3, k4 = Keyword(), Keyword(), Keyword(), Keyword()
k1.parents.update([k3, k4])
k2.children.update([k1, k3])

assert k1.parents == set([k2, k3, k4])
assert k2.parents == set([])
assert k3.parents == set([k2])
assert k4.children == set([k1])


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