Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#12137 closed (fixed)

Postgres 8.4 imposes strict typing on CharField and TextField

Reported by: Russell Keith-Magee Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


Consider the following model:

class MyObj(models.Model):
    CHOICES = (
        ('1', 'first choice')
        ('2', 'second choice')
    choice = models.CharField(max_length=1, choices=CHOICES)

(i.e, a char field whose choices are integers, but stored as strings. Now run two queries. First, query using an integer:


This yields the SQL:

('SELECT `myapp_myobj`.`id`, `myapp_myobj`.`choice` FROM `myapp_myobj` WHERE `myapp_myobj`.`choice` = %s ', (1,))

Now, query with an actual string:


which yields the SQL:

('SELECT `myapp_myobj`.`id`, `myapp_myobj`.`choice` FROM `myapp_myobj` WHERE `myapp_myobj`.`choice` = %s ', ('1',))

As reported by Bill Freeman on django-users, both examples work find for all databases -- except for Postgres 8.4. Postgres 8.4 imposes strong type checking, and raises an error on the first query.

The fact that the first example (the integer lookup) passes at all is due to the good grace of the databases themselves - logically, I think Postgres 8.4 is correct in declaring this an error. After all, "1" != 1.

I think the fix is pretty simple. CharField doesn't currently have a get_db_prep_value() method, and it should.

Compare and contrast with IntegerField or BooleanField - both these fields have get_db_prep_value() methods that cast the provided value to int() and bool(). CharField (and TextField for that matter) should do the same, but with unicode(). This would force the filter value of 1 into '1', which will be passed to the backend as a string, as it should be.

Attachments (1)

mfiedls.diff (888 bytes) - added by ke1g 9 years ago.
Strawman patch (against django 1.0.3, django/db/models/fields/

Download all attachments as: .zip

Change History (6)

comment:1 Changed 9 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

Changed 9 years ago by ke1g

Attachment: mfiedls.diff added

Strawman patch (against django 1.0.3, django/db/models/fields/

comment:2 Changed 9 years ago by ke1g

The patch mfields.diff, above, is somewhat tested against PostgreSQL's 8.4 and 8.1.11. That is, my project now works against both versions of the database, having looked at pages in several apps (pages cms, profiles, admin, a custom app).
It is above my djangoiness to know whether it would have been better to have used "str" than "unicode" in the patch (two places).
I certainly can't promise that it doesn't break something else, but it seems "correct" to me.

comment:3 Changed 9 years ago by Russell Keith-Magee

Has patch: set
Needs tests: set

comment:4 Changed 9 years ago by Carl Meyer

Resolution: fixed
Status: newclosed

Actually a dupe of #10015, fixed in r12150.

comment:5 Changed 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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