Opened 6 years ago

Closed 6 years ago

#12166 closed (duplicate)

Instance.delete() cascade deletes related objects with null=True

Reported by: oremj Owned by: nobody
Component: Uncategorized Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Server model relation:

server_model = models.ForeignKey('ServerModel', blank=True, null=True)

a = System.objects.all()[0]
b = ServerModels.objects.all()[0]


Both b and a are deleted.


 {'sql': u'SELECT `systems`.`id`, `systems`.`hostname`, `systems`.`serial`, `systems`.`operating_system_id`, `systems`.`server_model_id`, `systems`.`created_on`, `systems`.`updated_on`, `systems`.`oob_ip`, `systems`.`asset_tag`, `systems`.`notes`, `systems`.`licenses`, `systems`.`allocation_id`, `systems`.`system_rack_id`, `systems`.`rack_order`, `systems`.`switch_ports`, `systems`.`patch_panel_port`, `systems`.`oob_switch_port`, `systems`.`purchase_date`, `systems`.`purchase_price`, `systems`.`system_status_id`, `systems`.`change_password`, `systems`.`ram` FROM `systems` WHERE `systems`.`server_model_id` = 221 ',
 {'sql': u'UPDATE `systems` SET `server_model_id` = NULL WHERE `id` IN (1333)',
 {'sql': u'DELETE FROM `systems` WHERE `id` IN (1333)'},
 {'sql': u'DELETE FROM `server_models` WHERE `id` IN (221)'}]

I'm not seeing this problem on 1.1, but I am seeing it on 1.1.1 and later.

Change History (3)

comment:1 Changed 6 years ago by oremj

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

Nevermind, this also happens on 1.1.

comment:2 Changed 6 years ago by Suor

Having the same problem with:

class Blog(models.Model):
    last_comment = models.ForeignKey('BlogComment', blank = True, null = True, editable = False)

class BlogComment(CommonComment):
    blog = models.ForeignKey(Blog)

When deleting last comment the whole blog is deleted. Django should be able to behave in 'on delete set null' style.
Having this on 1.1

comment:3 Changed 6 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

More options for delete behavior is the subject of #7539. Right now Django just does CASCADE. If that isn't what you want there are ways to get around it, see for example:

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