Deletion error of model with generic relation to inherited model
|Reported by:||ramusus||Owned by:||nobody|
|Cc:||ramusus@…||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I found that if we have 2 simple models connected by GenericRelation:
from django.contrib.comments.models import Comment class NewComment(Comment): title = models.CharField(max_length=60) class PersonWithComments(models.Model): comments = generic.GenericRelation(NewComment, object_id_field='object_pk')
It's impossible to delete PersonWithComments instance. With postgresql_psycopg2 and mysql DB drivers I always get error:
Traceback (most recent call last): File "/home/ram/PycharmProjects/m/web_site/generic/tests.py", line 75, in test_inherited_models_delete p.delete() File "/home/ram/PycharmProjects/m/web_site/django/db/models/base.py", line 610, in delete delete_objects(seen_objs, using) File "/home/ram/PycharmProjects/m/web_site/django/db/models/query.py", line 1282, in delete_objects del_query.delete_batch_related(pk_list, using=using) File "/home/ram/PycharmProjects/m/web_site/django/db/models/sql/subqueries.py", line 69, in delete_batch_related self.do_query(f.m2m_db_table(), where, using=using) File "/home/ram/PycharmProjects/m/web_site/django/db/models/sql/subqueries.py", line 27, in do_query self.get_compiler(using).execute_sql(None) File "/home/ram/PycharmProjects/m/web_site/django/db/models/sql/compiler.py", line 727, in execute_sql cursor.execute(sql, params) File "/home/ram/PycharmProjects/m/web_site/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute return self.cursor.execute(query, args) DatabaseError: column "object_pk" does not exist LINE 1: DELETE FROM "generic_newcomment" WHERE ("object_pk" IN (E'23...
With sqlite3 everithing is ok! I don't know, may be this issue related to http://code.djangoproject.com/ticket/11263, but there tells nothing about deleting.
This error appears because fields 'object_pk' and 'content_type' are not in NewComment, they are in Comment model from Comments app. And there is no checks for these fields inside ContentTypes apps.
I attach tests for recreating this issue and patch for solving it.
Change History (17)
comment:1 Changed 6 years ago by russellm
- Needs documentation unset
- Needs tests unset
- Patch needs improvement set
- Triage Stage changed from Unreviewed to Accepted
comment:2 Changed 5 years ago by gabrielhurley
- Component changed from Contrib apps to contrib.contenttypes
comment:12 Changed 11 months ago by charettes
- Triage Stage changed from Accepted to Ready for checkin