Opened 10 years ago

Closed 10 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: no UI/UX: no


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')
>>> rev1 = ArticleRevision(parent=a, number=1, content='stuff goes here')
>>> 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 )

Change History (1)

comment:1 Changed 10 years ago by Alex Gaynor

Resolution: duplicate
Status: newclosed

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

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