I know the association table is required for the many-to-many data
model; what I'm trying to do is keep the Association Object out of my
TermData class. There's an example of how to do this in a M:N between
two different tables at
http://www.sqlalchemy.org/docs/05/ormtutorial.html#building-a-many-to-many-relation,
but I can't seem to get the same behavior with Parent = aliased
(TermData).
>>> post_keywords = Table('post_keywords', metadata,
... Column('post_id', Integer, ForeignKey('
posts.id')),
... Column('keyword_id', Integer, ForeignKey('
keywords.id'))
... )
>>> class BlogPost(Base):
... __tablename__ = 'posts'
...
... id = Column(Integer, primary_key=True)
... user_id = Column(Integer, ForeignKey('
users.id'))
... headline = Column(String(255), nullable=False)
... body = Column(Text)
...
... # many to many BlogPost<->Keyword
... keywords = relation('Keyword', secondary=post_keywords,
backref='posts')
...
... def __init__(self, headline, body, author):
... self.author = author
... self.headline = headline
... self.body = body
...
... def __repr__(self):
... return "BlogPost(%r, %r, %r)" % (self.headline, self.body,
self.author)
>>> class Keyword(Base):
... __tablename__ = 'keywords'
...
... id = Column(Integer, primary_key=True)
... keyword = Column(String(50), nullable=False, unique=True)
...
... def __init__(self, keyword):
... self.keyword = keyword