Related (for the case of using a non-association-object): SQLAlchemy Many-to-many ratio for a single table
Building many-to-many relationships is easy. Creating a many-to-many relationship in the same table is almost as easy as described in the above question.
Creating a many-to-many relationship with an association is also easy.
What I cannot find is the right way to combine the objects of association and many-to-many relationships when the left and right sides are the same table.
So, starting with a simple, naive and obviously wrong version, which I spent forever trying to massage into the correct version:
t_groups = Table('groups', metadata,
Column('id', Integer, primary_key=True),
)
t_group_groups = Table('group_groups', metadata,
Column('parent_group_id', Integer, ForeignKey('groups.id'), primary_key=True, nullable=False),
Column('child_group_id', Integer, ForeignKey('groups.id'), primary_key=True, nullable=False),
Column('expires', DateTime),
)
mapper(Group_To_Group, t_group_groups, properties={
'parent_group':relationship(Group),
'child_group':relationship(Group),
})
?