Hey guys,
I am playing around with SQLAlchemy (over Pylons) and encountering a
strange problem. I am using the adjacency list relationship concept to
represent nodes and their parents in a single table. I copied exactly
this code from the examples:

class Node(Base):
    __tablename__ = 'nodes'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('nodes.id'))
    data = Column(Unicode(50))

    children = relationship('Node',
        cascade="all",
        backref=backref("parent", remote_side='Node.id'),
        # tried with and without this:
        collection_class=attribute_mapped_collection('data'),
    )

My test is basically adding two nodes, like so:

n = Node()
n.id = 1
n.parent_id = 0
n.data = 'parent'
n2 = Node()
n2.id = 2
n2.parent_id = 1
n2.data = 'child'
Session.add(n)
Session.add(n2)
Session.commit()

When I run my test, I get the following exception raised from
connections.py:
sqlalchemy.exc.IntegrityError: (IntegrityError) (1452, 'Cannot add or
update a child row: a foreign key constraint fails (`db`.`nodes`,
CONSTRAINT `nodes_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `nodes`
(`id`))') 'INSERT INTO nodes (id, parent_id, data) VALUES (%s, %s,
%s)' (1, 0, 'parent')

Please, what am I missing here?

Thanks!

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