Opened 13 years ago

Closed 9 years ago

Last modified 9 years ago

#15579 closed New feature (fixed)

Please add support to delete specialization , while preserving base class instance (multi-table inheritance)

Reported by: Piotr Czachur Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Markus Holtermann, sokandpal@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Example:
I got multi-table inheritance: Base(Model) and Specific(Base). I got instance of Specific. Sometimes there is a need to delete only Specific leaving Base part in database. Currently if I call delete() on specialization, recursive delete takes place, so Base instance is being deleted too.

Change History (16)

comment:1 by Russell Keith-Magee, 13 years ago

Resolution: wontfix
Status: newclosed

This can be done right now by setting the parent link to None on the child object prior to deletion. This is at least as intuitive as any specific API for this feature that I can think of.

comment:2 by Piotr Czachur, 13 years ago

Resolution: wontfix
Status: closedreopened

It doesn't seem to work.

Please take a look on this sample code:

class List(models.Model):      
    name = models.CharField(max_length=10) 
  
class SpecificList(List):      
    specific_name = models.CharField(max_length=10) 

Let's create specialization instance

>>> s = SpecificList(name='bear', specific_name='sasquatch')
>>> s.save()
110315 11:48:35	   21 Query	INSERT INTO `list_list` (`name`) VALUES ('bear')
		   21 Query	commit
		   21 Query	SELECT ** FROM `list_specificlist` WHERE `list_specificlist`.`list_ptr_id` = 3  LIMIT 1
		   21 Query	INSERT INTO `list_specificlist` (`list_ptr_id`, `specific_name`) VALUES (3, 'sasquatch')
		   21 Query	commit

And now delete (upcast) it as you suggested:

>>> s.list_ptr = None
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/srv/django-bugi/nowa/mypro/django/db/models/fields/related.py", line 327, in __set__
    (instance._meta.object_name, self.field.name))
ValueError: Cannot assign None: "SpecificList.list_ptr" does not allow null values.

I also tried setting *_ptr_id to None and then delete:

>>> s.list_ptr_id = None
>>> s.delete()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/srv/django-bugi/nowa/mypro/django/db/models/base.py", line 577, in delete
    assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
AssertionError: SpecificList object can't be deleted because its list_ptr_id attribute is set to None.

This does not work either.

Do I misunderstood you or this feature just doesn't work?

comment:3 by Luke Plant, 13 years ago

Type: New feature

comment:4 by Luke Plant, 13 years ago

Severity: Normal

comment:5 by Julien Phalip, 13 years ago

Resolution: wontfix
Status: reopenedclosed

Please do not reopen a ticket closed by a core dev. If you would like to tackle this issue please bring it up to the django-developers mailing list.

comment:6 by Russell Keith-Magee, 13 years ago

Resolution: wontfix
Status: closedreopened
Triage Stage: UnreviewedAccepted

My apologies -- it appears that your original report was correct. I closed it because I thought I had code in production that was using the trick I suggested; however, when I checked, it turns out I'm reparenting a child, not deleting it.

comment:7 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Aymeric Augustin, 11 years ago

Status: reopenednew

comment:10 by Markus Holtermann, 9 years ago

Cc: Markus Holtermann added

comment:11 by Andriy Sokolovskiy, 9 years ago

Cc: sokandpal@… added
Has patch: set

comment:12 by Markus Holtermann, 9 years ago

Needs documentation: set

comment:13 by Andriy Sokolovskiy, 9 years ago

Needs documentation: unset

comment:14 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:15 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In 81c2d9f6:

Fixed #15579 -- Added ability to delete only child models in multi-table inheritance.

comment:16 by Tim Graham <timograham@…>, 9 years ago

In 71c638fc:

Fixed typo in refs #15579 comment.

Note: See TracTickets for help on using tickets.
Back to Top