avoid cascade delete

1,074 views
Skip to first unread message

Merrick

unread,
Nov 17, 2008, 2:21:05 AM11/17/08
to Django users
I have two models, links and groups. A Link has an optional foreign
key Group.

When I delete a Group, Django by default deletes all Links that
referenced the Group that is being deleted.

How do I avoid the default behavior that does a cascade delete. Of
course I could use the cursor but would am hoping there is some option
I don't know of for delete().

David Zhou

unread,
Nov 17, 2008, 2:34:31 AM11/17/08
to django...@googlegroups.com

David Zhou

unread,
Nov 17, 2008, 2:37:14 AM11/17/08
to django...@googlegroups.com

Another way is to consider using a many to many there, rather than a
one to many.

Randy Barlow

unread,
Nov 17, 2008, 10:46:28 AM11/17/08
to django...@googlegroups.com
On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <mer...@gmail.com>
declared:

Funny that you ask this question, because I just spent two days undoing
the Django cascading delete on my end. If you look at the delete()
method in the Django code, it only makes three calls. The first two
gather a list of all the objects that Django wants to delete.
Basically, I wrote a new delete() method that iterates through those
objects, finds all the references to the object that you want to delete
and sets them to null. It then saves them. After this, you need to
run those first two lines again to regather the objects you want to
delete (this time, only the one object should appear), and then you can
finally delete them.

I would argue this is more desireable than using clear() explicitly,
because you don't have to know anything about your models with this
method. Any time you have to remember to set a certain
relationship to null, you are bound to forget about another
relationship between your objects, and you'll get cascading delete on
something you didn't expect, and you won't have even noticed that it
happened!

--
Randy Barlow
http://electronsweatshop.com

David Zhou

unread,
Nov 17, 2008, 10:57:59 AM11/17/08
to django...@googlegroups.com
On Mon, Nov 17, 2008 at 10:46 AM, Randy Barlow <rba...@americanri.com> wrote:
>
> On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <mer...@gmail.com>
> declared:
>> I have two models, links and groups. A Link has an optional foreign
>> key Group.
>>
>> When I delete a Group, Django by default deletes all Links that
>> referenced the Group that is being deleted.
>>
>> How do I avoid the default behavior that does a cascade delete. Of
>> course I could use the cursor but would am hoping there is some option
>> I don't know of for delete().
>
> I would argue this is more desireable than using clear() explicitly,
> because you don't have to know anything about your models with this
> method. Any time you have to remember to set a certain
> relationship to null, you are bound to forget about another
> relationship between your objects, and you'll get cascading delete on
> something you didn't expect, and you won't have even noticed that it
> happened!

But that just means you'll need to explicitly set cascade on those
models you *do* want to cascade delete. And, IMO, the vast majority
of foreign key use cases do benefit from an auto cascading delete.

For the specific Links/Groups example, personally I'd just do it with
a many to many relation. It's entirely feasible that in the future,
Links will need the ability to associate itself to multiple groups.
For now, I'd just limit a link to one group via some custom
validation.

Merrick

unread,
Nov 17, 2008, 8:56:43 PM11/17/08
to Django users
Thank you for all of the advice, it is nice to have so many options. I
ended up using:

group.link_set.clear()
group.delete()

On Nov 17, 7:57 am, "David Zhou" <da...@nodnod.net> wrote:
> On Mon, Nov 17, 2008 at 10:46 AM, Randy Barlow <rbar...@americanri.com> wrote:
>
> > On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <merr...@gmail.com>
Reply all
Reply to author
Forward
0 new messages