Chris Mear
2006-Jun-07 05:36 UTC
#5209 patch: :dependent => :nullify deletes child records
Hi everyone, A couple of weeks ago I noticed a bug with :dependent => :nullify on a has_many or has_one. When you delete the parent, the children''s foreign keys are nullified, as expected. But when you do parent.child.delete or parent.children.clear, ActiveRecord actually deletes the child records, rather than just nullifying them. In my eyes, if you''ve set :dependent => :nullify, you''re saying that the child records should only ever be nullified, not deleted. The behaviour I''m seeing seems to be a hang over from when setting :dependent always meant ''delete/destroy the children''. However, I realise that maybe this is just my interpretation of what the :dependent => :nullify option is supposed to do. So I''d be interested to hear what everyone else thinks. I''ve written up the problem, with a patch, here: http://dev.rubyonrails.org/ticket/5209 Cheers, Chris
Chris Mear
2006-Jul-20 08:34 UTC
Re: #5209 patch: :dependent => :nullify deletes child records
On 7 Jun 2006, at 6:36 am, Chris Mear wrote:> A couple of weeks ago I noticed a bug with :dependent => :nullify > on a has_many or has_one. When you delete the parent, the > children''s foreign keys are nullified, as expected. But when you do > parent.child.delete or parent.children.clear, ActiveRecord actually > deletes the child records, rather than just nullifying them. > > In my eyes, if you''ve set :dependent => :nullify, you''re saying > that the child records should only ever be nullified, not deleted. > The behaviour I''m seeing seems to be a hang over from when > setting :dependent always meant ''delete/destroy the children''. > > However, I realise that maybe this is just my interpretation of > what the :dependent => :nullify option is supposed to do. So I''d be > interested to hear what everyone else thinks. > > I''ve written up the problem, with a patch, here: > > http://dev.rubyonrails.org/ticket/5209Any thoughts/opinions on this yet? Sorry to spam, but it''s potentially destructive behaviour, and I''m keen to see it resolved one way or the other. Thanks, Chris
Rimantas Liubertas
2006-Jul-20 08:55 UTC
Re: #5209 patch: :dependent => :nullify deletes child records
>> When you delete the parent, the > > children''s foreign keys are nullified, as expected. But when you do > > parent.child.delete or parent.children.clear, ActiveRecord actually > > deletes the child records, rather than just nullifying them. > > > > In my eyes, if you''ve set :dependent => :nullify, you''re saying > > that the child records should only ever be nullified, not deleted. > > The behaviour I''m seeing seems to be a hang over from when > > setting :dependent always meant ''delete/destroy the children''. > > > > However, I realise that maybe this is just my interpretation of > > what the :dependent => :nullify option is supposed to do. So I''d be > > interested to hear what everyone else thinks.<...>> Any thoughts/opinions on this yet? Sorry to spam, but it''s > potentially destructive behaviour, and I''m keen to see it resolved > one way or the other.It just do what you ask it to. :dependent specifies what to do with children when _parent_ is deleted. With call to parent.child.delete or parent.children.clear you specifically ask to delete children, and AR just does that. Regards, Rimantas -- http://rimantas.com/
Chris Mear
2006-Jul-20 12:09 UTC
Re: #5209 patch: :dependent => :nullify deletes child records
On 20 Jul 2006, at 9:55 am, Rimantas Liubertas wrote:> On 7 Jun 2006, at 6:36 am, Chris Mear wrote: > >> A couple of weeks ago I noticed a bug with :dependent => :nullify >> on a has_many or has_one. When you delete the parent, the >> children''s foreign keys are nullified, as expected. But when you >> do parent.child.delete or parent.children.clear, ActiveRecord >> actually deletes the child records, rather than just nullifying them > > It just do what you ask it to. :dependent specifies what to do with > children when _parent_ is deleted. > > With call to parent.child.delete or parent.children.clear you > specifically ask > to delete children, and AR just does that.Sorry, my slip. In my original post, I didn''t mean to say ''parent.child.delete''. Obviously, that should delete the child record. I meant to say ''parent.child = nil''. Apologies for confusing the issue. To restate my point: If you *haven''t* specified :dependent => :something, then when you call parent.children.clear or parent.child = nil, the children are nullified, not deleted. This is the default behaviour. But when you specify :dependent => :nullify, then, all of a sudden, calls to parent.children.clear or parent.child = nil result in the children being deleted, not nullified. In my eyes, that''s unexpected behaviour. I haven''t asked for my children to be deleted, and yet they are being deleted. Chris