Skip to content

Commit

Permalink
Fixed #35344, Refs #34838 -- Corrected output_field of resolved colum…
Browse files Browse the repository at this point in the history
…ns for GeneratedFields in aliased tables.

Thanks Simon Charette for the review.
  • Loading branch information
us77ipis committed Apr 1, 2024
1 parent 425b260 commit 5f18021
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/fields/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def cached_col(self):
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)

Expand Down
3 changes: 3 additions & 0 deletions docs/releases/5.0.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
14 changes: 12 additions & 2 deletions tests/model_fields/test_generatedfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ class Square(Model):
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):
Expand All @@ -134,7 +139,12 @@ class FloatSquare(Model):
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")
Expand Down

0 comments on commit 5f18021

Please sign in to comment.