﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
11631	Deleting related objects which use a GenericKey in an inherited model creates invalid SQL	stretch	nobody	"There seems to be an issue deleting related objects which use a GenericForeignKey stored in the table of an inherited model. For Example:

{{{
#!python
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:

{{{
#!python
>>> 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:

{{{
#!python
>>> 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 )
}}}
"		closed	Database layer (models, ORM)	1.1		duplicate			Unreviewed	0	0	0	0	0	0
