djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

HStoreDict does not convert long integers to strings. #60

Closed mattiaslinnap closed 10 years ago

mattiaslinnap commented 10 years ago

Python "long" integers are not converted to strings by HStoreDict.ensure_acceptable_value(). Usual integers are silently promoted to infinite-sized long integers by Python, and surprisingly long is not a subclass of int.

>>> from django_hstore.dict import HStoreDict as HSD
>>> HSD({'a': 123})
{'a': u'123'}
>>> HSD({'a': 12345678901234567890})
{'a': 12345678901234567890L}
>>> type(HSD({'a': 12345678901234567890})['a'])
<type 'long'>
>>> isinstance(1, int)
True
>>> isinstance(12345678901234567890, int)
False
>>> isinstance(12345678901234567890, long)
True

This can eventually result in a mixed string and integer PostgreSQL array being passed to the hstore() constructor, and PostgreSQL complaining about strings not being integers.

A proposed fix is to add "long" to the list of types in https://github.com/djangonauts/django-hstore/blob/1.3.0/django_hstore/dict.py#L123

mattiaslinnap commented 10 years ago

Also, I haven't looked much into the new Schema Mode yet, but isn't the if statement at https://github.com/djangonauts/django-hstore/blob/1.3.0/django_hstore/dict.py#L120 the wrong way around? Isn't converting everything to a string the right thing to do exactly when there is no schema?

nemesifier commented 10 years ago

@mattiaslinnap there's a whole block of comments, if you read it you will understand why it is coded that way. I'll check @Naddiseo's patch soon.

nemesifier commented 10 years ago

should be fixed, let us know.