Code

Opened 20 months ago

Last modified 18 months ago

#18889 new New feature

Provide a simple way to retain parent instances after children are deleted

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: ionel.mc@…, charette.s@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a Location table with two children (using table-inheritance): Restaurant and ChipShop. There should be a simple way to convert a ChipShop to a Restaurant without deleting/recreating the Location which may be referenced by lots of foreign keys, e.g:

location = Location.objects.get(1) # Currently a ChipShop
restaurant = Restaurant(location_ptr=location)
restaurant.save()
location.chipshop.delete() # Deletes the ChipShop row but not the Location

Currently this can be achieved by using raw sql to bypass django's code that deletes the parent when a child is deleted. Doing the following does not work as the parent is still deleted, perhaps this is a bug?

class ChipShop(Location);
   location_ptr = models.OneToOneField(Location, on_delete=DO_NOTHING, parent_link=True)

Modifying the on_delete of the created location_ptr does nothing as well. Perhaps a setting in the parents Meta that could be used to specify that the parent should not be deleted when the child is?

Attachments (0)

Change History (4)

comment:1 Changed 20 months ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 20 months ago by IonelMaries

  • Cc ionel.mc@… added

comment:3 Changed 18 months ago by akaariai

  • Triage Stage changed from Unreviewed to Accepted

I do think having some way to do this would be good. At least I have had need to do obj.delete(delete_parents=False). Not sure if that is the best API...

comment:4 Changed 18 months ago by charettes

  • Cc charette.s@… added
  • Version changed from 1.4 to master

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.