From 5da47e43c7cb051991a3248311248cf6ba7ff27d Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 30 Jun 2008 06:24:21 +0000 Subject: Fixed #7314 -- Changed the way extra() bits are handled when QuerySets are merged. Also added a section to the documentation to indicate why it's probably not a good idea to rely on this feature for complex stuff. Garbage in, garbage out applies even to Django code. Thanks to erik for the test case for this one. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7791 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 764a256006..6c06609969 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -366,10 +366,21 @@ class Query(object): item.relabel_aliases(change_map) self.select.append(item) self.select_fields = rhs.select_fields[:] - self.extra_select = rhs.extra_select.copy() - self.extra_tables = rhs.extra_tables - self.extra_where = rhs.extra_where - self.extra_params = rhs.extra_params + + if connector == OR: + # It would be nice to be able to handle this, but the queries don't + # really make sense (or return consistent value sets). Not worth + # the extra complexity when you can write a real query instead. + if self.extra_select and rhs.extra_select: + raise ValueError("When merging querysets using 'or', you " + "cannot have extra(select=...) on both sides.") + if self.extra_where and rhs.extra_where: + raise ValueError("When merging querysets using 'or', you " + "cannot have extra(where=...) on both sides.") + self.extra_select.update(rhs.extra_select) + self.extra_tables += rhs.extra_tables + self.extra_where += rhs.extra_where + self.extra_params += rhs.extra_params # Ordering uses the 'rhs' ordering, unless it has none, in which case # the current ordering is used. -- cgit v1.3