Opened 19 years ago
Closed 12 years ago
#3055 closed defect (fixed)
GenericRelation should squawk if pointed to a model that doesn't have a GenericForeignKey on it
| Reported by: | Owned by: | Marcos Moyano | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev | 
| Severity: | major | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | yes | 
| Easy pickings: | no | UI/UX: | no | 
Description
Originally discussed on this django-users thread.
I'm restating here the problem for future reference and for more comfort.
The problem happens when using the following structure:
class Tag(models.Model): ... class TaggedObject(models.Model): content_type = ... object_id = models.PositiveIntegerField("Object ID") tag = models.ForeignKey(Tag) content_object = models.GenericForeignKey() class UserTaggedObject(models.Model): user = ... object_tag = models.ForeignKey(TaggedObject) class ArticleAttachment(models.Model): ... tags = models.GenericRelation(TaggedObject) user_tags = models.GenericRelation(UserTaggedObject)
When deleting an ArticleAttachment (with delete()), I get an OperationalError exception (1054, "Unknown column 'object_id' in 'where clause'"). Drilling down through the backtrace, the offending SQL code is in:
django_src/django/db/backends/util.py in execute 12. return self.cursor.execute(sql, params) ... Local vars Variable Value params (5L,) self <django.db.backends.util.CursorDebugWrapper object at 0xb6cdda0c> sql 'DELETE FROM `tags_usertaggedobject` WHERE `object_id` IN (%s)'
which seems wrong to me, because object_id is in tags_taggedobject
and not tags_usertaggedobject.
Marco
Attachments (2)
Change History (19)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
comment:3 by , 15 years ago
| Summary: | Deleting an object with two generic relations to different but related models (via a ForeignKey) apparently generates incorrect SQL → GenericRelation should squawk if pointed to a model that doesn't have a GenericForeignKey on it | 
|---|
Jason's comment is correct. The SQL is wrong because the models are wrong. It's not legit to point a GenericRelation at a model which doesn't have a GenericForeignKey.
However, the result should not be bad SQL when you try to delete; GenericRelations should fail fast if they are invalid. Updating the summary accordingly.
comment:4 by , 15 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
by , 15 years ago
| Attachment: | ticket_30500_2.patch added | 
|---|
comment:5 by , 15 years ago
| Triage Stage: | Accepted → Ready for checkin | 
|---|
comment:6 by , 15 years ago
| Has patch: | set | 
|---|
Nit: consider changing slightly  the validation message to '"model '%s' must have a GenericForeignKey in order to create a GenericRelation pointing to it."' or similar.
Other that that, this is RFC.
comment:8 by , 15 years ago
comment:9 by , 14 years ago
| Easy pickings: | unset | 
|---|---|
| UI/UX: | unset | 
comment:11 by , 14 years ago
| Resolution: | fixed | 
|---|---|
| Status: | closed → reopened | 
comment:13 by , 14 years ago
| Triage Stage: | Ready for checkin → Accepted | 
|---|
Moving out of "ready for checkin" status, since the patch requires rethinking.
comment:15 by , 14 years ago
| Patch needs improvement: | set | 
|---|
comment:16 by , 13 years ago
| Status: | reopened → new | 
|---|
comment:17 by , 12 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
I believe this is invalid. GenericRelation assumes the model has content_type and object_id fields and will not follow the foreign key for content_type/object_id. The error is because it is trying to remove UserTaggedObjects since there is a GenericRelation in ArticleAttachment to it.