mirror of
				https://github.com/django/django.git
				synced 2025-10-28 08:06:09 +00:00 
			
		
		
		
	Fixed #35614 -- Prevented SQLCompiler.as_subquery_condition() from mutating a query.
This commit is contained in:
		
				
					committed by
					
						 Sarah Boyce
						Sarah Boyce
					
				
			
			
				
	
			
			
			
						parent
						
							b21f6d7ee4
						
					
				
				
					commit
					bdd538488c
				
			| @@ -1616,14 +1616,15 @@ class SQLCompiler: | |||||||
|     def as_subquery_condition(self, alias, columns, compiler): |     def as_subquery_condition(self, alias, columns, compiler): | ||||||
|         qn = compiler.quote_name_unless_alias |         qn = compiler.quote_name_unless_alias | ||||||
|         qn2 = self.connection.ops.quote_name |         qn2 = self.connection.ops.quote_name | ||||||
|  |         query = self.query.clone() | ||||||
|  |  | ||||||
|         for index, select_col in enumerate(self.query.select): |         for index, select_col in enumerate(query.select): | ||||||
|             lhs_sql, lhs_params = self.compile(select_col) |             lhs_sql, lhs_params = self.compile(select_col) | ||||||
|             rhs = "%s.%s" % (qn(alias), qn2(columns[index])) |             rhs = "%s.%s" % (qn(alias), qn2(columns[index])) | ||||||
|             self.query.where.add(RawSQL("%s = %s" % (lhs_sql, rhs), lhs_params), AND) |             query.where.add(RawSQL("%s = %s" % (lhs_sql, rhs), lhs_params), AND) | ||||||
|  |  | ||||||
|         sql, params = self.as_sql() |         sql, params = query.as_sql(compiler, self.connection) | ||||||
|         return "EXISTS (%s)" % sql, params |         return "EXISTS %s" % sql, params | ||||||
|  |  | ||||||
|     def explain_query(self): |     def explain_query(self): | ||||||
|         result = list(self.execute_sql()) |         result = list(self.execute_sql()) | ||||||
|   | |||||||
| @@ -223,6 +223,13 @@ class MultiColumnFKTests(TestCase): | |||||||
|             [m2], |             [m2], | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |     def test_query_does_not_mutate(self): | ||||||
|  |         """ | ||||||
|  |         Recompiling the same subquery doesn't mutate it. | ||||||
|  |         """ | ||||||
|  |         queryset = Friendship.objects.filter(to_friend__in=Person.objects.all()) | ||||||
|  |         self.assertEqual(str(queryset.query), str(queryset.query)) | ||||||
|  |  | ||||||
|     def test_select_related_foreignkey_forward_works(self): |     def test_select_related_foreignkey_forward_works(self): | ||||||
|         Membership.objects.create( |         Membership.objects.create( | ||||||
|             membership_country=self.usa, person=self.bob, group=self.cia |             membership_country=self.usa, person=self.bob, group=self.cia | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user