#23801 closed New feature (fixed)
Warn when max_length is used with IntegerField
Reported by: | David Eyk | Owned by: | Mattia Procopio |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | 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
An IntegerField with a max_length silently truncates integers. Observe:
>>> class MyModel(Model): >>> num = IntegerField(max_length=2) >>> m = MyModel.objects.create(num=42) >>> m.num 42 >>> m.num = 427 >>> m.num 427 >>> m.save() >>> m.num 42
This violates the Zen of Python: "Errors should never pass silently." It has also has been a reliable source of fun bugs over the years. This is long-standing Procrustean behavior, since at least v1.0 I think, if not before, but it bit me for the nth time yesterday, so I'm filing a bug.
Change History (14)
comment:1 by , 10 years ago
comment:3 by , 10 years ago
Summary: | IntegerField w/ max_length silently truncates integers → Warn when max_length is used with IntegerField |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → New feature |
comment:4 by , 10 years ago
What backend are you using? With SQLite and postgresql we cannot observe this behaviour (truncating numbers)
comment:5 by , 10 years ago
I'm using django.db.backends.postgresql_psycopg2
, and I've seen the behavior with Postgres 8.4 and 9.0. I should mention that we're on Django 1.4--I haven't tested this with a newer Django yet, but as I said, I've been aware of the behavior since 1.0 or so. My code session above is lightly edited from an actual session (obviously the model def needs to be in a file and there's a syncdb or migration implicit as well).
I'll try reproducing the behavior in Django 1.7 later, just to be sure.
comment:6 by , 10 years ago
Well bless me, now I can't reproduce this either, even on Django 1.1 and Postgres 8.4. Perhaps I've been taking crazy pills all this time? max_length
appears to have no effect whatsoever. Please feel free to invalidate this ticket, and I apologize for wasting your time!
comment:7 by , 10 years ago
Can't reproduce this against sqlite or postgresql (9.3) with django 1.7.1 final
comment:8 by , 10 years ago
Adding a warning could be still maybe ok to just let people know about that. Thoughts?
comment:9 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:10 by , 10 years ago
I wrote a small patch for this, you can find it here. https://github.com/MattBlack85/django/compare/gh-mb-fix_23801?expand=1
Is it ok for the intent?
comment:11 by , 10 years ago
Has patch: | set |
---|
Hi, Do you want to hit the "Create pull request" button?
comment:12 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:13 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I am not sure how to proceed given in #22637 "the opinion was that the
max_length
option should not be forbidden in order to keep Django upward compatible, but that it also does not make sense for theIntegerField
. I suppose we could add a warning using the checks framework (which could be silenced by those intentionally usingmax_length
onIntegerField
for some reason). Do you have any suggestions?