Skip to content

Commit 6025eab

Browse files
nileshpaharijacobtylerwalls
authored andcommitted
Fixed #36806 -- Added system check for null kwarg in GeneratedField.
The null argument has no effect on GeneratedField since the nullability of the column depends on the database and expression used.
1 parent afaa527 commit 6025eab

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

django/db/models/fields/generated.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, *, expression, output_field, db_persist, **kwargs):
3030
self.expression = expression
3131
self.output_field = output_field
3232
self.db_persist = db_persist
33+
self.has_null_arg = "null" in kwargs
3334
super().__init__(**kwargs)
3435

3536
@cached_property
@@ -82,6 +83,7 @@ def check(self, **kwargs):
8283
*super().check(**kwargs),
8384
*self._check_supported(databases),
8485
*self._check_persistence(databases),
86+
*self._check_ignored_options(databases),
8587
]
8688
output_field_clone = self.output_field.clone()
8789
output_field_clone.model = self.model
@@ -188,6 +190,20 @@ def _check_persistence(self, databases):
188190
)
189191
return errors
190192

193+
def _check_ignored_options(self, databases):
194+
warnings = []
195+
196+
if self.has_null_arg:
197+
warnings.append(
198+
checks.Warning(
199+
"null has no effect on GeneratedField.",
200+
obj=self,
201+
id="fields.W225",
202+
)
203+
)
204+
205+
return warnings
206+
191207
def deconstruct(self):
192208
name, path, args, kwargs = super().deconstruct()
193209
del kwargs["blank"]

docs/ref/checks.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ Model fields
221221
``GeneratedField``\s.
222222
* **fields.E223**: ``GeneratedField.output_field`` has errors: ...
223223
* **fields.W224**: ``GeneratedField.output_field`` has warnings: ...
224+
* **fields.W225**: ``null`` has no effect on ``GeneratedField``.
224225
* **fields.E900**: ``IPAddressField`` has been removed except for support in
225226
historical migrations.
226227
* **fields.W900**: ``IPAddressField`` has been deprecated. Support for it

docs/ref/models/fields.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,9 @@ materialized view.
13771377
backends that support it (SQLite, PostgreSQL, and Oracle) and marked as
13781378
deferred otherwise.
13791379

1380+
:attr:`~Field.null` has no effect on ``GeneratedField`` since whether the
1381+
column is nullable depends on the database and expression used.
1382+
13801383
``GenericIPAddressField``
13811384
-------------------------
13821385

tests/invalid_models_tests/test_ordinary_fields.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,3 +1510,26 @@ class Model(models.Model):
15101510
Model._meta.get_field("field").check(databases={"default"}),
15111511
expected_warnings,
15121512
)
1513+
1514+
@skipUnlessDBFeature("supports_stored_generated_columns")
1515+
def test_with_null_argument(self):
1516+
class Model(models.Model):
1517+
value = models.IntegerField()
1518+
field = models.GeneratedField(
1519+
expression=models.F("value") * 2,
1520+
output_field=models.IntegerField(),
1521+
db_persist=True,
1522+
null=True,
1523+
)
1524+
1525+
expected_warnings = [
1526+
DjangoWarning(
1527+
"null has no effect on GeneratedField.",
1528+
obj=Model._meta.get_field("field"),
1529+
id="fields.W225",
1530+
),
1531+
]
1532+
self.assertEqual(
1533+
Model._meta.get_field("field").check(databases={"default"}),
1534+
expected_warnings,
1535+
)

0 commit comments

Comments
 (0)