Code

Opened 5 years ago

Closed 5 years ago

#11631 closed (duplicate)

Deleting related objects which use a GenericKey in an inherited model creates invalid SQL

Reported by: stretch Owned by: nobody
Component: Database layer (models, ORM) 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:

Description

There seems to be an issue deleting related objects which use a GenericForeignKey stored in the table of an inherited model. For Example:

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class WikiObjectBase(models.Model):
    
    name = models.CharField('Name', max_length=200, unique=True)
    
    class Meta:
        abstract = True

class WikiRevision(models.Model):
    
    parent_type = models.ForeignKey(ContentType, editable=False)
    parent_id = models.PositiveIntegerField(editable=False)
    parent = generic.GenericForeignKey('parent_type', 'parent_id')
    number = models.IntegerField('Number', default=1, editable=False)

class ArticleRevision(WikiRevision):
    
    content = models.TextField()

class Article(WikiObjectBase):
    
    revisions = generic.GenericRelation('ArticleRevision', content_type_field='parent_type', object_id_field='parent_id')

class ImageRevision(WikiRevision):
    
    content = models.TextField()

class Image(WikiObjectBase):
    
    revisions = generic.GenericRelation('ImageRevision', content_type_field='parent_type', object_id_field='parent_id')

Creating objects works fine:

>>> a = Article(name='Foo')
>>> a.save()
>>> rev1 = ArticleRevision(parent=a, number=1, content='stuff goes here')
>>> rev1.save()
>>> a
<Article: Article object>
>>> rev1
<ArticleRevision: ArticleRevision object>

However, attempting to delete the Article object results in an invalid query:

>>> a.delete()
...
OperationalError: (1054, "Unknown column 'parent_id' in 'where clause'")

The SQL query is constructed as though the parent_type and parent_id columns exist within the ArticleRevision model's table, rather than in the inherited table:

DELETE FROM `mytest_articlerevision` WHERE (`parent_id` IN (1) AND `parent_type_id` = 42 )

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of a previous ticket (which I can't find right now).

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


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

 
Note: See TracTickets for help on using tickets.