On Apr 20, 2011, at 12:33 AM, Aviv Giladi wrote: > Hey guys, > > I reproduced the problem within my controllers, but I also ran this > test (after fully loading my environment of course): > > parent = Node() > parent.id = 1 > parent.parent_id = None > parent.name = 'parent' > Session.add(parent) > > child = Node() > child.id = 20 > child.parent_id = 1 > child.name = 'child' > Session.add(child) > > Session.commit() > foreign key constraint fails (`db`.`nodes`, CONSTRAINT > `nodes_ibfk_1` FOREIGN KEY > (`parent_id`) REFERENCES `nodes` (`id`))') 'INSERT INTO nodes (id, > parent_id, name) VALUES > (%s, %s, %s)' (20, 0, 'child') > The problem arises when I change the `parent` node's id to 0 (and the > `child`'s parent_id to 0 accordingly). Then, I get the following > exception:
if you update the value of a primary key column that's referenced by foreign keys to a new value, all referencing foreign keys must be updated simultaneously, that is, within the single UPDATE statement. You use ON UPDATE CASCADE with your database to achieve this. That said, a primary key value of "0" is a poor choice - you should stick with 1-based integer primary key values. The actual value in a so-called surrogate primary key should also never be significant within the application. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
