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 Tim Graham, 3 months ago

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.

comment:2 by Sarah Boyce, 3 months ago

Cc: Lily Foote added
Triage Stage: UnreviewedAccepted

comment:3 by Lily Foote, 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 Tim Graham, 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 Tim Graham, 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 Sarah Boyce, 6 weeks ago

Owner: set to Tim Graham
Status: newassigned

comment:7 by Sarah Boyce, 6 weeks ago

Triage Stage: AcceptedReady for checkin

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 6 weeks ago

Resolution: fixed
Status: assignedclosed

In c4614b5:

Fixed #35815 -- Made system check accept db_default literals when DatabaseFeatures.supports_expression_defaults = False.

Note: See TracTickets for help on using tickets.
Back to Top