Django

Code

Ticket #10829 (new)

Opened 1 year ago

Last modified 5 months ago

QuerySet.delete() attempts to delete unmanaged models.

Reported by: justinlilly Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: 1.1-beta-1 Keywords: unmanaged
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

When there is an unmanaged model which has a ForeignKey to an object that you're attempting to delete, the ORM attempts to delete it. In my case, this is a database view which doesn't support deletion.

I'll update this with a bit more information and a method for reproducing the error later today, just want to get it on the radar.

I'm also assigning this to the 1.1 milestone as not being able to delete an object associated with a managed model is kind of a big deal. Feel free to override me on it though.

Attachments

django.db.models.query.py.patch (2.0 kB) - added by philroche on 05/27/09 06:55:52.
Patch for MySQL unmanaged view (discussed in comment#7)

Change History

(follow-up: ↓ 8 ) 04/15/09 17:13:21 changed by Alex

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Hrm, this will require some thought, as an unmanaged model *could* indicate something like a db view that you can't delete from, however that semantic is in no way implied by the documentation.

04/15/09 17:13:26 changed by Alex

  • stage changed from Unreviewed to Design decision needed.

04/16/09 22:23:23 changed by anonymous

I would say that the proper way to handle this would be to have a kwarg for the delete method. QuerySet?.delete(cascade=False) Seems reasonable, at least.

04/16/09 22:31:17 changed by mtredinnick

Urgh. This is messy. The idea in comment:3 isn't a solution, since this isn't a property of the delete() call -- it's a property of the model definition (and cascading delete behaviour is the subject of another ticket and unresolved as yet because it's a hard problem to cover all use-cases). The current behaviour is correct (unmanaged refers to syncdb and other SQL-altering statements), but we should be able to handle Justin's case, too.

Would be nice to have a solution for 1.1, so I'll give this some thought.

05/06/09 16:42:30 changed by justinlilly

For any looking for a stop-gap, I've gotten past it by writing a delete() method which invalidates the FK used to populate my view which then allows it to delete cleanly. Code is:

    def delete(self):
        baseid = self.baseobject_ptr_id
        BaseRelation.objects.filter(models.Q(base_obj=baseid) | models.Q(related_obj=baseid)).delete()
        super(Video, self).delete()

05/07/09 04:34:17 changed by jacob

  • milestone deleted.

This is probably a subset of the "deletes always cascade" bug, but even if not this won't be fixed in the 1.1 timeframe.

05/08/09 09:13:46 changed by philroche

This is a show stopper for us so I have a small patch

line 1002 django\db\models\query.py

perform_delete = True

if cls._meta.managed == False:
    check_view = "select count(TABLE_NAME) from INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = '%s';" % cls._meta.db_table
    cursor = connection.cursor()
    cursor.execute(check_view, [])
    if not cursor.rowcount == 0:
        perform_delete = False
    
if perform_delete:
   ...... the rest of the deletion of code

It's ugly as hell I know but a start.

05/27/09 06:55:52 changed by philroche

  • attachment django.db.models.query.py.patch added.

Patch for MySQL unmanaged view (discussed in comment#7)

(in reply to: ↑ 1 ) 10/27/09 11:48:46 changed by dvainsencher

Replying to Alex:

Hrm, this will require some thought, as an unmanaged model *could* indicate something like a db view that you can't delete from, however that semantic is in no way implied by the documentation.

I think that if you create a structure that can represent a view it's API should know how to deal with database changes operations. It's look like a bug to me.


Add/Change #10829 (QuerySet.delete() attempts to delete unmanaged models.)




Change Properties
Action