#36806: Add system check for null kwarg in GeneratedField definition
-------------------------------------+-------------------------------------
Reporter: Dai-Tado | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):
* summary: GeneratedField with null=False creates nullable column on MySQL
=> Add system check for null kwarg in GeneratedField definition
* type: Bug => Cleanup/optimization
Comment:
Good idea, I would be happy to accept something similar to what we have
for `ManyToManyField`, e.g.:
{{{#!diff
diff --git a/django/db/models/fields/generated.py
b/django/db/models/fields/generated.py
index f89269b5e6..551302475b 100644
--- a/django/db/models/fields/generated.py
+++ b/django/db/models/fields/generated.py
@@ -30,6 +30,7 @@ class GeneratedField(Field):
self.expression = expression
self.output_field = output_field
self.db_persist = db_persist
+ self.has_null = "null" in kwargs
super().__init__(**kwargs)
@cached_property
@@ -82,6 +83,7 @@ class GeneratedField(Field):
*super().check(**kwargs),
*self._check_supported(databases),
*self._check_persistence(databases),
+ *self._check_ignored_options(databases),
]
output_field_clone = self.output_field.clone()
output_field_clone.model = self.model
@@ -188,6 +190,20 @@ class GeneratedField(Field):
)
return errors
+ def _check_ignored_options(self, **kwargs):
+ warnings = []
+
+ if self.has_null_arg:
+ warnings.append(
+ checks.Warning(
+ "null has no effect on GeneratedField.",
+ obj=self,
+ id="fields.WXXX",
+ )
+ )
+
+ return warnings
+
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
del kwargs["blank"]
}}}
----
Would you like to submit a PR and add a test? You should be able to riff
on the docs & tests for ManyToMany from
011abb7d96c75f6154c15a8a0f997d8c27698679 (#19671).
--
Ticket URL: <
https://code.djangoproject.com/ticket/36806#comment:3>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.