Steve Zatz wrote: > > I have a simple foreign key relationship between a Task class and a > Context class, where many Tasks can have the same Context. The > default value for the Task foreign key context_id is 0. When I delete > a Context, the Tasks with that context have their context_id > automatically set to None and I would like it to be set to 0. (I > believe this is the default cascade behavior as I do not have any > cascade set on the mapper.) Right now I then explicitly change each > affected Tasks context_id to 0. Do I have to do this explicitly, or > is there a way on delete of a Context to have the task updated to a > context_id = 0. Thanks for any advice.
if you're using foreign keys correctly, that would imply there's an entity with an id of "0", and you'd attach that Context to each Task, replacing the old Context to be deleted. Otherwise if "0" isn't a real ID, and I guess you're using a non-consistent DB like sqlite or MyISAM (and you have some great reason to be doing this in the first place), the rough equivalent would be to detach the to-be-deleted context and then assign "0" to the context_id manually. So yes SQLA's "NULL" setting behavior is a hardwired thing since we target a certain model of persistence with regards to foreign keys - for other arrangements you'd have to do that yourself. It is easy enough to do within a function that iterates through the list. You can automate the calling of this function by building a SessionExtension, implementing before_flush(), and searching through the session.deleted collection for Context objects to be processed. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---