summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-08-20 12:46:14 -0400
committerTim Graham <timograham@gmail.com>2015-09-05 08:19:38 -0400
commit62347208bb0cef6990699d7373fa06e2e12ead07 (patch)
tree843d89b762630dc6a290d98edc0aa26763b5fada
parentf1d6b5b5b1c8a3461e7bb90760531cf5725b59be (diff)
[1.8.x] Fixed #24525 -- Fixed AssertionError in some complex queries.
Thanks Anssi Kääriäinen for providing the solution. Backport of 2dc9ec5616a942de3a0886a707f93988f56dd594 from master
-rw-r--r--django/db/models/sql/query.py3
-rw-r--r--docs/releases/1.8.5.txt2
-rw-r--r--tests/queries/models.py1
-rw-r--r--tests/queries/tests.py24
4 files changed, 27 insertions, 3 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 7a0fe8f892..40b6cc57a1 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -539,7 +539,8 @@ class Query(object):
# distinct joins for the same connection in rhs query, then the
# combined query must have two joins, too.
reuse.discard(new_alias)
- change_map[alias] = new_alias
+ if alias != new_alias:
+ change_map[alias] = new_alias
if not rhs.alias_refcount[alias]:
# The alias was unused in the rhs query. Unref it so that it
# will be unused in the new query, too. We have to add and
diff --git a/docs/releases/1.8.5.txt b/docs/releases/1.8.5.txt
index ac1735bb16..66cee6e25c 100644
--- a/docs/releases/1.8.5.txt
+++ b/docs/releases/1.8.5.txt
@@ -14,6 +14,8 @@ Bugfixes
* Fixed ``AssertionError`` in some delete queries with a model containing a
field that is both a foreign and primary key (:ticket:`24951`).
+* Fixed ``AssertionError`` in some complex queries (:ticket:`24525`).
+
* Fixed a migrations crash with ``GenericForeignKey`` (:ticket:`25040`).
* Made ``translation.override()`` clear the overridden language when a
diff --git a/tests/queries/models.py b/tests/queries/models.py
index 5d63ae9ea8..81f3b3f390 100644
--- a/tests/queries/models.py
+++ b/tests/queries/models.py
@@ -45,6 +45,7 @@ class Tag(models.Model):
class Note(models.Model):
note = models.CharField(max_length=100)
misc = models.CharField(max_length=10)
+ tag = models.ForeignKey(Tag, blank=True, null=True)
class Meta:
ordering = ['note']
diff --git a/tests/queries/tests.py b/tests/queries/tests.py
index 6ba799d244..84871e8dfe 100644
--- a/tests/queries/tests.py
+++ b/tests/queries/tests.py
@@ -1177,6 +1177,15 @@ class Queries1Tests(BaseQuerysetTest):
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[0].category, RemovedInDjango19Warning))
+ def test_lookup_constraint_fielderror(self):
+ msg = (
+ "Cannot resolve keyword 'unknown_field' into field. Choices are: "
+ "annotation, category, category_id, children, id, item, "
+ "managedmodel, name, note, parent, parent_id"
+ )
+ with self.assertRaisesMessage(FieldError, msg):
+ Tag.objects.filter(unknown_field__name='generic')
+
class Queries2Tests(TestCase):
@classmethod
@@ -1319,8 +1328,8 @@ class Queries4Tests(BaseQuerysetTest):
generic = NamedCategory.objects.create(name="Generic")
cls.t1 = Tag.objects.create(name='t1', category=generic)
- n1 = Note.objects.create(note='n1', misc='foo', id=1)
- n2 = Note.objects.create(note='n2', misc='bar', id=2)
+ n1 = Note.objects.create(note='n1', misc='foo')
+ n2 = Note.objects.create(note='n2', misc='bar')
e1 = ExtraInfo.objects.create(info='e1', note=n1)
e2 = ExtraInfo.objects.create(info='e2', note=n2)
@@ -1335,6 +1344,17 @@ class Queries4Tests(BaseQuerysetTest):
Item.objects.create(name='i1', created=datetime.datetime.now(), note=n1, creator=cls.a1)
Item.objects.create(name='i2', created=datetime.datetime.now(), note=n1, creator=cls.a3)
+ def test_ticket24525(self):
+ tag = Tag.objects.create()
+ anth100 = tag.note_set.create(note='ANTH', misc='100')
+ math101 = tag.note_set.create(note='MATH', misc='101')
+ s1 = tag.annotation_set.create(name='1')
+ s2 = tag.annotation_set.create(name='2')
+ s1.notes = [math101, anth100]
+ s2.notes = [math101]
+ result = math101.annotation_set.all() & tag.annotation_set.exclude(notes__in=[anth100])
+ self.assertEqual(list(result), [s2])
+
def test_ticket11811(self):
unsaved_category = NamedCategory(name="Other")
with six.assertRaisesRegex(self, ValueError,