mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[5.0.x] Refs #33482 -- Fixed QuerySet selecting and filtering againts Exists() with empty queryset.
Thanks Tobias Bengfort for the report.
Backport of ea596a52d9 from main
			
			
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							d9ab9dbea6
						
					
				
				
					commit
					458bc9e768
				
			| @@ -1624,6 +1624,15 @@ class Exists(Subquery): | |||||||
|             sql = "CASE WHEN {} THEN 1 ELSE 0 END".format(sql) |             sql = "CASE WHEN {} THEN 1 ELSE 0 END".format(sql) | ||||||
|         return sql, params |         return sql, params | ||||||
|  |  | ||||||
|  |     def as_sql(self, compiler, *args, **kwargs): | ||||||
|  |         try: | ||||||
|  |             return super().as_sql(compiler, *args, **kwargs) | ||||||
|  |         except EmptyResultSet: | ||||||
|  |             features = compiler.connection.features | ||||||
|  |             if not features.supports_boolean_expr_in_select_clause: | ||||||
|  |                 return "1=0", () | ||||||
|  |             return compiler.compile(Value(False)) | ||||||
|  |  | ||||||
|  |  | ||||||
| @deconstructible(path="django.db.models.OrderBy") | @deconstructible(path="django.db.models.OrderBy") | ||||||
| class OrderBy(Expression): | class OrderBy(Expression): | ||||||
|   | |||||||
| @@ -2294,6 +2294,14 @@ class ExistsTests(TestCase): | |||||||
|         self.assertSequenceEqual(qs, [manager]) |         self.assertSequenceEqual(qs, [manager]) | ||||||
|         self.assertIs(qs.get().not_exists, True) |         self.assertIs(qs.get().not_exists, True) | ||||||
|  |  | ||||||
|  |     def test_filter_by_empty_exists(self): | ||||||
|  |         manager = Manager.objects.create() | ||||||
|  |         qs = Manager.objects.annotate(exists=Exists(Manager.objects.none())).filter( | ||||||
|  |             pk=manager.pk, exists=False | ||||||
|  |         ) | ||||||
|  |         self.assertSequenceEqual(qs, [manager]) | ||||||
|  |         self.assertIs(qs.get().exists, False) | ||||||
|  |  | ||||||
|  |  | ||||||
| class FieldTransformTests(TestCase): | class FieldTransformTests(TestCase): | ||||||
|     @classmethod |     @classmethod | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user