Re: avoid cascade delete
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" <[EMAIL PROTECTED]> wrote: > On Mon, Nov 17, 2008 at 10:46 AM, Randy Barlow <[EMAIL PROTECTED]> wrote: > > > On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <[EMAIL PROTECTED]> > > 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. > > -- > --- > David Zhou > [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: avoid cascade delete
On Mon, Nov 17, 2008 at 10:46 AM, Randy Barlow <[EMAIL PROTECTED]> wrote: > > On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <[EMAIL PROTECTED]> > 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. -- --- David Zhou [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: avoid cascade delete
On Sun, 16 Nov 2008 23:21:05 -0800 (PST), Merrick <[EMAIL PROTECTED]> 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(). 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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: avoid cascade delete
On Mon, Nov 17, 2008 at 2:34 AM, David Zhou <[EMAIL PROTECTED]> wrote: > On Mon, Nov 17, 2008 at 2:21 AM, Merrick <[EMAIL PROTECTED]> wrote: >> >> 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(). > > Try using clear() prior to deleting: > > http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward Another way is to consider using a many to many there, rather than a one to many. -- --- David Zhou [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: avoid cascade delete
On Mon, Nov 17, 2008 at 2:21 AM, Merrick <[EMAIL PROTECTED]> wrote: > > 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(). Try using clear() prior to deleting: http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward -- --- David Zhou [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---