Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#7159 closed (wontfix)

Patch to add a --delete option to loaddata management command

Reported by: Graham King <graham@…> Owned by: nobody
Component: Tools Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The attached patch adds a '--delete' option to the loaddata command. This deletes any objects from the database that are not in the fixture. The fixture must contain at least one object of the class for the delete to run.

The result is that loaddata can now insert/update/delete to make a database match the fixture, becoming a sort of 'syncdata' command. A 'dumpdata' followed by 'loaddata --delete' effectively publishes one database onto another. We use this to publish our staging system to live.

The patch contains changes to management/commands/, to docs/django-admin.txt and to tests/modeltests/fixtures/ File fixture4.json goes in tests/modeltests/fixtures/fixtures/.

Attachments (3)

loaddata-delete.diff (5.4 KB) - added by Graham King <graham@…> 11 years ago.
The patch
fixture4.json (606 bytes) - added by Graham King <graham@…> 11 years ago.
Fixture for the test case
loaddata-delete-2.diff (5.3 KB) - added by Graham King <graham@…> 11 years ago.
Patch updated for Django r7899

Download all attachments as: .zip

Change History (7)

Changed 11 years ago by Graham King <graham@…>

Attachment: loaddata-delete.diff added

The patch

Changed 11 years ago by Graham King <graham@…>

Attachment: fixture4.json added

Fixture for the test case

comment:1 Changed 11 years ago by Jeff Anderson

Triage Stage: UnreviewedDesign decision needed

Changed 11 years ago by Graham King <graham@…>

Attachment: loaddata-delete-2.diff added

Patch updated for Django r7899

comment:2 Changed 11 years ago by Graham King <graham@…>

loaddata-delete-2.diff works as is with r8075 (1.0 Alpha).

comment:3 Changed 11 years ago by Russell Keith-Magee

Resolution: wontfix
Status: newclosed

The way the feature is described in help text and options, it is equivalent to ' flush', followed by ' loaddata', which makes the proposed option redundant.

However, looking at the implementation, it is doing something quite different, and much more concerning. All you would need is one Foo object to accidentally get dropped into a fixture, and you lose all of your Foo data in the database. Alternately, you could have a model dependency between a model you _do_ want to flush, and one that you _dont_ want to flush; the cascading deletes from this option could cause you to lose data outside the classes that you think are going to be removed. I'm not in favour of adding commands or options that could lead to unexpected data loss on the scale that this command has the potential to do.

comment:4 Changed 11 years ago by Graham King <graham@…>

Thanks for taking the time to review this patch - I know 1.0 is keeping everyone very busy.

It is a bit like ' reset', followed by ' loaddata'. 'flush' doesn't work if you are loading accounts from the auth app.
The contenttypes initial data gets re-created, so after a flush the loaddata might fail because auth_permission.content_type_id foreign key points at a non-existent row.

I understand your concerns, if someone wanted to load only part of their database, and somehow specified --delete. This patch only applies for loading the entire database.

I have moved this to Django Snippets as a new 'syncdata' command:
The downside is lots of the 'loaddata' code is duplicated.

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