Adrian von Bidder wrote:
> Heyho!
>
> How can I use Declarative to create self-referencing stuff?  I'm trying
> without success to create the tree example (Node with Node.parent and
> Node.children) in declarative.
>
> The basic table is:
>
> class Node(Base):
>     __tablename__ = 'nodes'
>
>     id = Column(Integer, primary_key = True)
>     parent_id = Column('parent', Integer, ForeignKey('nodes.id'))
>
> Now I'm struggling with how to add a relation so that I can use
> mynode.parent and mynode.children.
>
> I've tried
> parent = relation(
>       'Node', backref=backref('children', remote_side=[Node.id]))
> and various variants but always ended up with an exception (different ones
> for various cases.)
>
> I guess I have to use remote_side somehow but I couldn't figure how.

class Node(Base):
    __tablename__ = 'nodes'

    id = Column(Integer, primary_key = True)
    parent_id = Column('parent', Integer, ForeignKey('nodes.id'))
    parent = relation("Node", remote_side=id, backref="children")



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