Opened 3 months ago
Closed 6 weeks ago
#35815 closed Bug (fixed)
System check for default database values with expressions prohibits non-expressions
Reported by: | Tim Graham | Owned by: | Tim Graham |
---|---|---|---|
Component: | Core (System checks) | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Lily Foote | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Since its introduction in Django 5.0, the fields.E011 system check for database backends that have DatabaseFeatures.supports_expression_defaults = False
requires literal defaults to be wrapped in Value.
There are a number of test models that have int, float and string db_defaults that will raise a system check error if DatabaseFeatures.supports_expression_defaults = False
since these models don't have required_db_features = {"supports_expression_defaults"}
.
I'm working on MongoDB which doesn't support any database defaults, literal or expressions.
Change History (8)
comment:1 by , 3 months ago
comment:2 by , 3 months ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 3 months ago
I think either solution could work, but maybe there's a subtlety I don't remember. I think wrapping all literals in Value
is most likely to just work.
comment:4 by , 2 months ago
The issue I see with requiring Value
wrapping is that third-party apps may not know to do it and therefore run into compatibility issues with backends with supports_expression_defaults = False
. For the other solution I suggested, I'm not sure we can make the isinstance()
check exhaustive for all possible non-expression types.
Perhaps it's more appropriate to replace isinstance(self.db_default, Value)
with not hasattr(self.db_default, "resolve_expression")
to reject expressions.
comment:5 by , 6 weeks ago
Has patch: | set |
---|
PR to prevent the system check from rejecting Field.db_default
literals (integer, float, boolean, etc.) when DatabaseFeatures.supports_expression_defaults = False
.
comment:6 by , 6 weeks ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:7 by , 6 weeks ago
Triage Stage: | Accepted → Ready for checkin |
---|
I'm not sure if the appropriate solution is to add
Value
wrapping to all literal defaults in Django's test models or to modify the isinstance(db_default, Value) check to also accept int/float, str, etc.