Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#12664 closed (fixed)

GenericRelation fails if originating model has custom primary key

Reported by: jbronn Owned by: jbronn
Component: Contrib apps Version: master
Severity: Keywords: generic genericrelation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation:
Needs tests: Patch needs improvement:
Easy pickings: UI/UX:

Description

Example models, note that Person model has a field with primary_key=True:

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

class Address(models.Model):
    street = models.CharField(max_length=80)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=2)
    zipcode = models.CharField(max_length=5)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

    def __unicode__(self):
        return '%s %s, %s %s' % (self.street, self.city, self.state, self.zipcode)

class Person(models.Model):
    account = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=128)
    addresses = generic.GenericRelation(Address)

    def __unicode__(self):
        return self.name

Create some test data and query on addresses generic relation:

>>> p = Person.objects.create(account=23, name='Chef')
>>> a = Address.objects.create(street='123 Anywhere Place', city='Conifer', state='CO', zipcode='80433', content_object=p)
>>> Person.objects.filter(addresses__zipcode='80433')
Traceback (most recent call last):
  File "/Users/jbronn/hg/django/trunk/tests/regressiontests/generic_relations_regress/tests.py", line 31, in test_reverse_relation_pk
    self.assertEqual(1, qs.count())
  File "/Users/jbronn/hg/django/trunk/django/db/models/query.py", line 324, in count
    return self.query.get_count(using=self.db)
  File "/Users/jbronn/hg/django/trunk/django/db/models/sql/query.py", line 369, in get_count
    number = obj.get_aggregation(using=using)[None]
  File "/Users/jbronn/hg/django/trunk/django/db/models/sql/query.py", line 341, in get_aggregation
    result = query.get_compiler(using).execute_sql(SINGLE)
  File "/Users/jbronn/hg/django/trunk/django/db/models/sql/compiler.py", line 674, in execute_sql
    cursor.execute(sql, params)
ProgrammingError: column generic_relations_regress_address.account does not exist
LINE 1: ...R JOIN "generic_relations_regress_address" T3 ON ("generic_r...

Attachments (1)

generic_relation_pk_fix.1.diff (3.0 KB) - added by jbronn 5 years ago.

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by jbronn

comment:1 Changed 5 years ago by jbronn

  • Resolution set to fixed
  • Status changed from new to closed

(In [12276]) Fixed #12664 -- Fixed GenericRelation.m2m_reverse_name to return the correct pk column name.

comment:2 Changed 5 years ago by jbronn

(In [12277]) [1.1.X] Fixed #12664 -- Fixed GenericRelation.m2m_reverse_name to return the correct pk column name.

Backport of r12276 from trunk.

comment:3 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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