Opened 2 days ago
Last modified 3 hours ago
#36806 assigned Cleanup/optimization
Add system check for null kwarg in GeneratedField definition
| Reported by: | Dai-Tado | Owned by: | Nilesh Pahari |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.2 |
| Severity: | Normal | Keywords: | GeneratedField |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Summary
GeneratedField ignores null=False on MySQL - columns are always nullable
Description
When creating a GeneratedField with null=False on MySQL, the database column is always created as nullable, ignoring the null=False parameter.
Steps to Reproduce
pythonfrom django.db import models
class MyModel(models.Model):
area = models.GeneratedField(
.....
null=False, # ← Ignored
)
- The migration file removes null=False from GeneratedField definition
- Even if manually added to migration file, Django doesn't apply NOT NULL constraint to database
Change History (9)
comment:1 by , 2 days ago
| Summary: | GeneratedField with null=False creates nullable column on MySQL with spatial types (POINT) → GeneratedField with null=False creates nullable column on MySQL |
|---|
comment:2 by , 2 days ago
comment:3 by , 2 days ago
| Summary: | GeneratedField with null=False creates nullable column on MySQL → Add system check for null kwarg in GeneratedField definition |
|---|---|
| Type: | Bug → Cleanup/optimization |
Good idea, I would be happy to accept something similar to what we have for ManyToManyField, e.g.:
-
django/db/models/fields/generated.py
diff --git a/django/db/models/fields/generated.py b/django/db/models/fields/generated.py index f89269b5e6..551302475b 100644
a b class GeneratedField(Field): 30 30 self.expression = expression 31 31 self.output_field = output_field 32 32 self.db_persist = db_persist 33 self.has_null = "null" in kwargs 33 34 super().__init__(**kwargs) 34 35 35 36 @cached_property … … class GeneratedField(Field): 82 83 *super().check(**kwargs), 83 84 *self._check_supported(databases), 84 85 *self._check_persistence(databases), 86 *self._check_ignored_options(databases), 85 87 ] 86 88 output_field_clone = self.output_field.clone() 87 89 output_field_clone.model = self.model … … class GeneratedField(Field): 188 190 ) 189 191 return errors 190 192 193 def _check_ignored_options(self, **kwargs): 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.WXXX", 202 ) 203 ) 204 205 return warnings 206 191 207 def deconstruct(self): 192 208 name, path, args, kwargs = super().deconstruct() 193 209 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).
comment:4 by , 2 days ago
| Keywords: | GeneratedField added |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:5 by , 43 hours ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:6 by , 24 hours ago
| Needs documentation: | set |
|---|---|
| Needs tests: | set |
comment:7 by , 5 hours ago
| Has patch: | set |
|---|
comment:9 by , 3 hours ago
| Needs documentation: | unset |
|---|---|
| Needs tests: | unset |
If this is the expected behavior, would it be possible to include this in the official documentation?