Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-10 Thread Russell Keith-Magee
On Thu, Jun 10, 2010 at 2:53 PM, Thomas Guettler  wrote:
> The documentation says django emulates "ON DELETE CASCADE":
>   http://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects
>
> But it is missing how to emulate ON DELETE SET NULL.
>
> In this thread Kevin Howerton posted an abstract base class to emulate ON
> DELETE SET NULL. But this does not work if you bulk delete with a query set.
>
> I opened a new ticket:
>
> http://code.djangoproject.com/ticket/13731

Kevin's abstract base class is certainly a novel way of solving the
problem (mod the issues with bulk delete). However, the real fix is to
add first-class support for alternate deletion strategies. This is the
subject of #7539.

If someone wants to take on #7539 as a task for 1.3, they certainly
have my blessing, and I'd certainly do anything I can to help. It's
been on the low priority list for the last couple of releases, and on
my personal to-do for much longer. The last time I looked at the
patch, it was starting to look reasonably good. However, it's not
finished yet -- it really needs someone who is suitably motivated to
carry it the final mile.

In the interim, I'm not in favor of adding a 'workaround' solution as
part of Django's official documentation. This sort of workaround is
something that should live on Django Snippets.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-10 Thread Peter Bengtsson
Maybe the documentation is deliberately missing it because it's not
supposed to have to be documented since it's supposed to just work.
But as I'm suspecting, it doesn't.

On 10 June 2010 02:53, Thomas Guettler  wrote:
> The documentation says django emulates "ON DELETE CASCADE":
>   http://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects
>
> But it is missing how to emulate ON DELETE SET NULL.
>
> In this thread Kevin Howerton posted an abstract base class to emulate ON
> DELETE SET NULL. But this does not work if you bulk delete with a query set.
>
> I opened a new ticket:
>
> http://code.djangoproject.com/ticket/13731
>
>  Thomas
>
> Russell Keith-Magee wrote:
>> On Wed, Jun 9, 2010 at 3:53 AM, Peter Bengtsson  wrote:
>>> On 8 June 2010 13:09, Jeremy Dunck  wrote:
 On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
> I've now had to learn this the hard way by having real live data
> deleted from my database on two production projects and it pisses me
> off big time every time.
>
> I can accept that NOT nullable foreign relations cascade the delete
> but not if they have null=True on them. Example:
 Actually, this looks to be fixed in 1.2.  What version are you
 running?  Closed ticket (with test cases) here:
 http://code.djangoproject.com/ticket/9308

>>> I'm running Django 1.2.1 so perhaps it's not fixed.
>>> Sigh. I have to make a test case to prove it.
>>
>> Looking at Trac, #9308 was fixed in r10822, so it should be fixed in
>> 1.1, and it was ported back to 1.0.X as well.
>>
>> We pretty clearly document that our deletion scheme emulates "ON
>> DELETE CASCADE"; if you can provide evidence to the contrary, please
>> open a ticket.
>>
>> Yours,
>> Russ Magee %-)
>>
>
> --
> Thomas Guettler, http://www.thomas-guettler.de/
> E-Mail: guettli (*) thomas-guettler + de
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django developers" group.
> To post to this group, send email to django-develop...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-developers+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-developers?hl=en.
>
>



-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
fun crosstips.org

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-10 Thread Harro
Also ON DELETE RESTRICT

On Jun 10, 8:53 am, Thomas Guettler  wrote:
> The documentation says django emulates "ON DELETE CASCADE":
>    http://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects
>
> But it is missing how to emulate ON DELETE SET NULL.
>
> In this thread Kevin Howerton posted an abstract base class to emulate ON
> DELETE SET NULL. But this does not work if you bulk delete with a query set.
>
> I opened a new ticket:
>
> http://code.djangoproject.com/ticket/13731
>
>   Thomas
>
>
>
>
>
> Russell Keith-Magee wrote:
> > On Wed, Jun 9, 2010 at 3:53 AM, Peter Bengtsson  wrote:
> >> On 8 June 2010 13:09, Jeremy Dunck  wrote:
> >>> On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
>  I've now had to learn this the hard way by having real live data
>  deleted from my database on two production projects and it pisses me
>  off big time every time.
>
>  I can accept that NOT nullable foreign relations cascade the delete
>  but not if they have null=True on them. Example:
> >>> Actually, this looks to be fixed in 1.2.  What version are you
> >>> running?  Closed ticket (with test cases) here:
> >>>http://code.djangoproject.com/ticket/9308
>
> >> I'm running Django 1.2.1 so perhaps it's not fixed.
> >> Sigh. I have to make a test case to prove it.
>
> > Looking at Trac, #9308 was fixed in r10822, so it should be fixed in
> > 1.1, and it was ported back to 1.0.X as well.
>
> > We pretty clearly document that our deletion scheme emulates "ON
> > DELETE CASCADE"; if you can provide evidence to the contrary, please
> > open a ticket.
>
> > Yours,
> > Russ Magee %-)
>
> --
> Thomas Guettler,http://www.thomas-guettler.de/
> E-Mail: guettli (*) thomas-guettler + de

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-10 Thread Thomas Guettler
The documentation says django emulates "ON DELETE CASCADE":
   http://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects

But it is missing how to emulate ON DELETE SET NULL.

In this thread Kevin Howerton posted an abstract base class to emulate ON
DELETE SET NULL. But this does not work if you bulk delete with a query set.

I opened a new ticket:

http://code.djangoproject.com/ticket/13731

  Thomas

Russell Keith-Magee wrote:
> On Wed, Jun 9, 2010 at 3:53 AM, Peter Bengtsson  wrote:
>> On 8 June 2010 13:09, Jeremy Dunck  wrote:
>>> On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
 I've now had to learn this the hard way by having real live data
 deleted from my database on two production projects and it pisses me
 off big time every time.

 I can accept that NOT nullable foreign relations cascade the delete
 but not if they have null=True on them. Example:
>>> Actually, this looks to be fixed in 1.2.  What version are you
>>> running?  Closed ticket (with test cases) here:
>>> http://code.djangoproject.com/ticket/9308
>>>
>> I'm running Django 1.2.1 so perhaps it's not fixed.
>> Sigh. I have to make a test case to prove it.
> 
> Looking at Trac, #9308 was fixed in r10822, so it should be fixed in
> 1.1, and it was ported back to 1.0.X as well.
> 
> We pretty clearly document that our deletion scheme emulates "ON
> DELETE CASCADE"; if you can provide evidence to the contrary, please
> open a ticket.
> 
> Yours,
> Russ Magee %-)
> 

-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-09 Thread Russell Keith-Magee
On Wed, Jun 9, 2010 at 3:53 AM, Peter Bengtsson  wrote:
> On 8 June 2010 13:09, Jeremy Dunck  wrote:
>> On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
>>> I've now had to learn this the hard way by having real live data
>>> deleted from my database on two production projects and it pisses me
>>> off big time every time.
>>>
>>> I can accept that NOT nullable foreign relations cascade the delete
>>> but not if they have null=True on them. Example:
>>
>> Actually, this looks to be fixed in 1.2.  What version are you
>> running?  Closed ticket (with test cases) here:
>> http://code.djangoproject.com/ticket/9308
>>
> I'm running Django 1.2.1 so perhaps it's not fixed.
> Sigh. I have to make a test case to prove it.

Looking at Trac, #9308 was fixed in r10822, so it should be fixed in
1.1, and it was ported back to 1.0.X as well.

We pretty clearly document that our deletion scheme emulates "ON
DELETE CASCADE"; if you can provide evidence to the contrary, please
open a ticket.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-08 Thread Peter Bengtsson
On 8 June 2010 13:09, Jeremy Dunck  wrote:
> On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
>> I've now had to learn this the hard way by having real live data
>> deleted from my database on two production projects and it pisses me
>> off big time every time.
>>
>> I can accept that NOT nullable foreign relations cascade the delete
>> but not if they have null=True on them. Example:
>
> Actually, this looks to be fixed in 1.2.  What version are you
> running?  Closed ticket (with test cases) here:
> http://code.djangoproject.com/ticket/9308
>
I'm running Django 1.2.1 so perhaps it's not fixed.
Sigh. I have to make a test case to prove it.

> --
> You received this message because you are subscribed to the Google Groups 
> "Django developers" group.
> To post to this group, send email to django-develop...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-developers+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-developers?hl=en.
>
>



-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
fun crosstips.org

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-08 Thread Kevin Howerton
This abstract base class likely is a bit less performant than having
in-db support for cascades in pgsql... though it should give you the
behavior you are seeking.

It should set to default, or null if allowed.

Enjoy.

-k

class ClearOnDelete(models.Model):
   def delete(self):
   related_objects = self._meta.get_all_related_objects()
   for object in related_objects:
   accessor_name = object.get_accessor_name()
   related_accessor = getattr(self.__class__, accessor_name)
   related_accessor_instance = getattr(self, accessor_name)

   if related_accessor.related.field.default is not NOT_PROVIDED:
   for relation in related_accessor_instance.all():
   setattr(relation,
related_accessor.related.field.name,
related_accessor.related.field.default)
   relation.save()

   elif related_accessor.related.field.null:
   for relation in related_accessor_instance.all():
   setattr(relation, related_accessor.related.field.name, None)
   relation.save()
   super(ClearOnDelete, self).delete()

   class Meta:
   abstract = True

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-08 Thread Jeremy Dunck
On Tue, Jun 8, 2010 at 7:30 AM, Peter Bengtsson  wrote:
> I've now had to learn this the hard way by having real live data
> deleted from my database on two production projects and it pisses me
> off big time every time.
>
> I can accept that NOT nullable foreign relations cascade the delete
> but not if they have null=True on them. Example:

Actually, this looks to be fixed in 1.2.  What version are you
running?  Closed ticket (with test cases) here:
http://code.djangoproject.com/ticket/9308

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Re: Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-08 Thread Thomas Guettler
Hi,

I think this is the related ticket:
http://code.djangoproject.com/ticket/7539  "Add ON DELETE and ON UPDATE support 
to Django"

Me and a lot of other people are on the cc list of this ticket.

Peter Bengtsson wrote:
> I've now had to learn this the hard way by having real live data
> deleted from my database on two production projects and it pisses me
> off big time every time.
> 
> I can accept that NOT nullable foreign relations cascade the delete
> but not if they have null=True on them. 
> ...

-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.



Beating on an old issue; counter intuitive cascade deletes on foreign keys

2010-06-08 Thread Peter Bengtsson
I've now had to learn this the hard way by having real live data
deleted from my database on two production projects and it pisses me
off big time every time.

I can accept that NOT nullable foreign relations cascade the delete
but not if they have null=True on them. Example:

class Survey(Models):
...

class Analysis(Models):
survey = ForeignKey(Survey, null=True)

Just looking at that it's to be fairly obvious that on deleting a
Survey instance it should nullify the foreign key relation on the
Analysis model. Not delete it!


On a perhaps unrelated note is it a bug that I can't prevent this with
signals? This doesn't work:

def disconnect(sender, instance, **kwargs):
for each in Analysis.objects.filter(survey=instance):
each.survey = None
each.save()

pre_delete.connect(disconnect, sender=Survey)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.