Opened 3 weeks ago

Closed 11 days ago

#37024 closed Cleanup/optimization (fixed)

Integer SITE_ID check incorrect when a different primary key type is used

Reported by: Tim Graham Owned by: Tim Graham
Component: contrib.sites Version: dev
Severity: Normal Keywords:
Cc: MANAS MADESHIYA Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

To catch a programmer mistake where the SITE_ID setting is defined with an incorrect type (e.g. SITE_ID = "1"), #31802 added a system check that only allows SITE_ID to be an int or None. This forces third-party databases with non-integer primary keys like MongoDB (which uses ObjectId) to silence this check.

It would be more appropriate if this check performed the validation based on Site._meta.pk rather than with hardcoded types.

I thought of a few options:

  1. Use Site._meta.pk.to_python(settings.SITE_ID) to validate the SITE_ID, relying on it to raise ValidationError for unexpected types. This won't work because AutoField.to_python() accepts strings that coerce to integer which are values the original fix was intended to reject.
  1. Use Site._meta.pk.to_python(settings.SITE_ID) but also check if settings.SITE_ID != Site._meta.pk.to_python(settings.SITE_ID). This would catch invalid values that to_python() coerces to the correct type.
  1. Add Field.expected_type which could be int, ObjectId, or whatever. The implementation of the check could continue to use isinstance(). The downside is that adding a new attribute to the Field API would be non-trivial.

I've implemented option #2.

Change History (4)

comment:1 by Tim Graham, 3 weeks ago

Has patch: set

comment:2 by Mariusz Felisiak, 3 weeks ago

Triage Stage: UnreviewedAccepted

comment:3 by MANAS MADESHIYA, 3 weeks ago

Cc: MANAS MADESHIYA added

comment:4 by GitHub <noreply@…>, 11 days ago

Resolution: fixed
Status: assignedclosed

In 179aa21:

Fixed #37024 -- Made SITE_ID system check validation use Site._meta.pk.

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