0
0
mirror of https://github.com/django/django.git synced 2024-11-21 19:09:18 +01:00

Fixed #35344, Refs #34838 -- Corrected output_field of resolved columns for GeneratedFields in aliased tables.

Thanks Simon Charette for the review.
This commit is contained in:
Johannes Westphal 2024-03-30 22:14:15 +00:00
parent 425b26092f
commit 5f18021640
4 changed files with 17 additions and 3 deletions

View File

@ -508,6 +508,7 @@ answer newbie questions, and generally made Django that much better:
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
Johan C. Stöver <johan@nilling.nl>
Johann Queuniet <johann.queuniet@adh.naellia.eu>
Johannes Westphal <jojo@w-hat.de>
john@calixto.net
John D'Agostino <john.dagostino@gmail.com>
John D'Ambrosio <dambrosioj@gmail.com>

View File

@ -39,7 +39,7 @@ class GeneratedField(Field):
return Col(self.model._meta.db_table, self, self.output_field)
def get_col(self, alias, output_field=None):
if alias != self.model._meta.db_table and output_field is None:
if alias != self.model._meta.db_table and output_field in (None, self):
output_field = self.output_field
return super().get_col(alias, output_field)

View File

@ -21,3 +21,6 @@ Bugfixes
* Fixed a bug in Django 5.0 that caused a migration crash on PostgreSQL 15+
when adding a partial ``UniqueConstraint`` with ``nulls_distinct``
(:ticket:`35329`).
* Fixed a crash in Django 5.0 when performing queries involving table aliases
and lookups on a ``GeneratedField`` of the aliased table (:ticket:`35344`).

View File

@ -123,7 +123,12 @@ class BaseGeneratedFieldTests(SimpleTestCase):
db_persist=True,
)
col = Square._meta.get_field("area").get_col("alias")
field = Square._meta.get_field("area")
col = field.get_col("alias")
self.assertIsInstance(col.output_field, IntegerField)
col = field.get_col("alias", field)
self.assertIsInstance(col.output_field, IntegerField)
class FloatSquare(Model):
@ -134,7 +139,12 @@ class BaseGeneratedFieldTests(SimpleTestCase):
output_field=FloatField(),
)
col = FloatSquare._meta.get_field("area").get_col("alias")
field = FloatSquare._meta.get_field("area")
col = field.get_col("alias")
self.assertIsInstance(col.output_field, FloatField)
col = field.get_col("alias", field)
self.assertIsInstance(col.output_field, FloatField)
@isolate_apps("model_fields")