title: SQLAlchemy - Parent child relationship in the same table
body: <p>I want to have a parent child relationship in the same table. Below is what I have, and it isn't working. By "not working", I meant that when I call <code>.children</code> on some <code>Item</code> object, it gives me <code>[]</code>. What is the error in my code and how to do it correctly? Thanks.</p>
  
  <pre><code>parents_children = Table('parents_children', Base.metadata,
                           Column('parent_id', bigint, ForeignKey('items.id', name='a'), primary_key=True),
                           Column('child_id', bigint, ForeignKey('items.id', name='b'), primary_key=True)
                           )
  
  class Item(Base):
      __tablename__ = 'items'
      id = Column(bigint, primary_key=True, autoincrement=True)
      name = Column(types.String(80), default="")
      display_name = Column(types.String(80), default="")
      version = Column(types.String(80), default="")
      discriminator = Column(item_tag_enum, nullable=False)
      children = relationship('Item',
                              secondary=parents_children,
                              primaryjoin=id == parents_children.c.parent_id,
                              secondaryjoin=id == parents_children.c.child_id,
                              backref='parents')
      __mapper_args__ = {'polymorphic_on' : discriminator}
  
   # Item subclasses omitted because that's irrelevant to the question.
  </code></pre>