Opened 5 years ago

Closed 5 years ago

#16747 closed Bug (wontfix)

IntegerField could validate too large numbers to work with PostgreSQL and MySQL be default

Reported by: Petr Marhoun <petr.marhoun@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Be default (without some additional validation) integer fields in Django do not checks any intervals for inserted numbers. So for example, if someone try official tutorial from Django documentation and use recommended database (PostgreSQL), he can get uncatched exception ("DatabaseError: integer out of range"). It would be nice to validate this range - it is problem not only for PostgreSQL but also for MySQL (but I do not know if these two databases are so important that they should define interval common for all databases).

BigIntegerField in Django checks interval [2 63, 2 63 - 1] so for consistency IntegerField could check interval [2 31, 2 31 -1]. Also, for other main fields (for example char fields or float fields or decimal fields or date fields) it is quite difficult or impossible to get uncatched exceptions - but it is really easy for integer fields.

I prepare patch with two tests - one (for BigIntegerField) passes on all tested databases (PostgreSQL 8.4.8, MySQL 5.1.54 with InnoDB, SQlite from Python 2.7.1 - everything on Ubuntu 11.04 64bit). The almost same second one (for IntegerField) failes on PostgreSQL with "DatabaseError: integer out of range" and on MySQL with "AssertionError" (saved number is truncated).

Attachments (1)

too-large-integers.diff (3.2 KB) - added by Petr Marhoun <petr.marhoun@…> 5 years ago.

Download all attachments as: .zip

Change History (2)

Changed 5 years ago by Petr Marhoun <petr.marhoun@…>

Attachment: too-large-integers.diff added

comment:1 Changed 5 years ago by Karen Tracey

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: wontfix
Status: newclosed

I don't think we can just add range checking to least, I don't see how we can do it in a backwards-compatible way. SQLite will happily store values outside of the PostgreSQL/MySQL range for integers. If we add this validation to IntegerField then suddenly we break what used to work fine when using SQLite as the DB. (The model form validation that is there for BigIntegerField didn't have this problem because it was added concurrently with BigIntegerField itself.) If you have a suggestion for how to solve this in a backwards-compatible way feel free to reopen; I agree the current situation is less than ideal but I'm not seeing how to fix it.

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