Code

#20378 closed Bug (fixed)

Reverse join fails when GenericRelation is defined on an abstract model

Reported by: gwahl@… Owned by: akaariai
Component: contrib.contenttypes Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you define a GenericRelation on an abstract model:

class HasLinks(models.Model):
    links = generic.GenericRelation(Link)

    class Meta:
        abstract = True

class HasLinkThing(HasLinks):
    pass

You should still be able to query using the reverse join:

HasLinkThing.objects.filter(links=whatever)

But it fails with when trying to calculate the join columns, because it's trying to get the pk of the abstract, instead of concrete, model:

Traceback (most recent call last):
  File ".tox/py27-dj16/src/django/tests/generic_relations_regress/tests.py", line 145, in test_abstract_reverse_join
    self.assertQuerysetEqual(HasLinkThing.objects.filter(links=link), [
  File ".tox/py27-dj16/src/django/django/db/models/manager.py", line 163, in filter
    return self.get_queryset().filter(*args, **kwargs)
  File ".tox/py27-dj16/src/django/django/db/models/query.py", line 661, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File ".tox/py27-dj16/src/django/django/db/models/query.py", line 679, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File ".tox/py27-dj16/src/django/django/db/models/sql/query.py", line 1200, in add_q
    clause = self._add_q(where_part, used_aliases)
  File ".tox/py27-dj16/src/django/django/db/models/sql/query.py", line 1236, in _add_q
    current_negated=current_negated)
  File ".tox/py27-dj16/src/django/django/db/models/sql/query.py", line 1102, in build_filter
    allow_explicit_fk=True)
  File ".tox/py27-dj16/src/django/django/db/models/sql/query.py", line 1365, in setup_joins
    connection = alias, opts.db_table, join.join_field.get_joining_columns()
  File ".tox/py27-dj16/src/django/django/db/models/fields/related.py", line 862, in get_joining_columns
    return self.field.get_reverse_joining_columns()
  File ".tox/py27-dj16/src/django/django/db/models/fields/related.py", line 1009, in get_reverse_joining_columns
    return self.get_joining_columns(reverse_join=True)
  File ".tox/py27-dj16/src/django/django/contrib/contenttypes/generic.py", line 199, in get_joining_columns
    return super(GenericRelation, self).get_joining_columns(reverse_join)
  File ".tox/py27-dj16/src/django/django/db/models/fields/related.py", line 1005, in get_joining_columns
    source = self.reverse_related_fields if reverse_join else self.related_fields
  File ".tox/py27-dj16/src/django/django/db/models/fields/related.py", line 980, in reverse_related_fields
    return [(rhs_field, lhs_field) for lhs_field, rhs_field in self.related_fields]
  File ".tox/py27-dj16/src/django/django/db/models/fields/related.py", line 975, in related_fields
    self._related_fields = self.resolve_related_fields()
  File ".tox/py27-dj16/src/django/django/contrib/contenttypes/generic.py", line 178, in resolve_related_fields
    self.to_fields = [self.model._meta.pk.name]
AttributeError: 'NoneType' object has no attribute 'name'

I've written a failing test to demonstrate: https://github.com/fusionbox/django/commit/03aa282467b. This test passes in django 1.5.1, but fails on master, so I believe this is a regression introduced by #19385.

Attachments (0)

Change History (2)

comment:1 Changed 12 months ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to akaariai
  • Patch needs improvement unset
  • Severity changed from Normal to Release blocker
  • Status changed from new to assigned
  • Triage Stage changed from Unreviewed to Accepted

Seems like a release blocker. I will try to fix this as soon as possible, but unfortunately I don't have time in the next two weeks. So, if anybody else wants to try to fix this feel free to do so...

comment:2 Changed 11 months ago by Anssi Kääriäinen <akaariai@…>

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

In c9a96075fa02b6d52bec748ffdfb413688a15774:

Fixed #20378 -- regression in GenericRelation on abstract model

When a GenericRelation was defined on abstract model, queries on childs
of the abstract model didn't work. The problem was in the way fields and
in particular field.rel was copied from models to their children.

The regression was likely caused by #19385. Thanks to Gavin Wahl for
spotting the regression.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.