querying hstore field

72 views
Skip to first unread message

Marcela Campo

unread,
Jul 2, 2014, 10:47:58 AM7/2/14
to django...@googlegroups.com
Hi,
  I am trying to use django-hstore for the first time and I am encountering some issues. I cannot figure out if I am doing something wrong or it is just an egg limitation.

I am storing a dictionary which values can be either string or numeric. Even when I knew that hstore handled everything as string, I was under the impression that django-hstore performed type casting at some point. I somehow inferred that from the documentation page where the examples for gt,lt,gte and lte are given for numbers (correctly speaking, strings that only contained numbers). However when testing with some examples I noticed that it always compares using string comparison, and a query with:

'info__gte':{'Perimeter': 1416},'info__lte':{'Perimeter': 2517}}

yields results

whereas this combination

'info__gte':{'Perimeter': 416},'info__lte':{'Perimeter': 2517}}

yields no results at all, and should at least return the same result set than the previous query

I am doing something wrong or it's just not supported by django-hstore?

Thanks in advance
Marcela

Max Demars

unread,
Jul 2, 2014, 12:00:12 PM7/2/14
to django...@googlegroups.com
Hi Marcela,

for your information hstore field can only store string values. Probably this is why you have your errors.

-Max Demars


--
You received this message because you are subscribed to the Google Groups "django-hstore" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-hstor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

nemesis

unread,
Jul 2, 2014, 12:28:59 PM7/2/14
to django...@googlegroups.com
On Wed, 2 Jul 2014 07:47:58 -0700 (PDT), Marcela Campo
<marcel...@gmail.com> wrote:
> Hi,
> I am trying to use django-hstore for the first time and I am
> encountering some issues. I cannot figure out if I am doing something
> wrong or it is just an egg limitation.
>
> I am storing a dictionary which values can be either string or
> numeric. Even when I knew that hstore handled everything as string, I
> was under the impression that django-hstore performed type casting at
> some point. I somehow inferred that from the documentation page where
> the examples for gt,lt,gte and lte are given for numbers (correctly
> speaking, strings that only contained numbers).

No, comparisons are done as strings.

Strings can be compared too.

'3' > '2' == True
'2' > '3' == False

but you could get

'a' > '1' == True

so pay attention.

It's possible to implement a comparison with some typecasting on the DB
level, at least according to what a guy I met at DjangoVillage in
orvieto told me.

Fed.

Marcela Campo

unread,
Jul 2, 2014, 3:48:24 PM7/2/14
to django...@googlegroups.com
Hi Fed, thanks for the clarification. I naively assumed it would perform numeric comparisons as examples where numbers in the docs pages.

I have tested comparisons in postgresql casting the values, for example 

SELECT info->'Perimeter' FROM ui_environments WHERE CAST(info->'Perimeter' AS decimal) > 2000;

which yields the correct results.

I've also seen ruby gems and a proposal for sqlalchemy to allow this (https://groups.google.com/forum/#!topic/sqlalchemy/uBNY-2RAKR4), the downside is that you have to declare the keys and datatypes in your python class first (in my case that's not possible because I don't know the data I'll be storing beforehand).

Thanks again for the information!
Marcela





--
You received this message because you are subscribed to a topic in the Google Groups "django-hstore" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-hstore/gvojz9ZSN-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-hstore+unsubscribe@googlegroups.com.

Nemesis

unread,
Jul 3, 2014, 12:34:04 PM7/3/14
to django...@googlegroups.com
On 07/02/2014 09:48 PM, Marcela Campo wrote:
> Hi Fed, thanks for the clarification. I naively assumed it would
> perform numeric comparisons as examples where numbers in the docs pages.
>
> I have tested comparisons in postgresql casting the values, for example
>
> SELECT info->'Perimeter' FROM ui_environments WHERE
> CAST(info->'Perimeter' AS decimal) > 2000;
>
> which yields the correct results.
>
> I've also seen ruby gems and a proposal for sqlalchemy to allow this
> (https://groups.google.com/forum/#!topic/sqlalchemy/uBNY-2RAKR4
> <https://groups.google.com/forum/#%21topic/sqlalchemy/uBNY-2RAKR4>),
> the downside is that you have to declare the keys and datatypes in
> your python class first (in my case that's not possible because I
> don't know the data I'll be storing beforehand).
>

I'm working on the possibility to specify the schema of the HStore field
beforehand in order to support types.

I'm telling just FYI.

> Thanks again for the information!
>

Welcome.

Federico

Marcela Campo

unread,
Jul 4, 2014, 2:17:28 PM7/4/14
to django...@googlegroups.com
That would be great, I just saw the new feature discussion in GitHub, I am looking forward it!


Thanks
Marcela


--
You received this message because you are subscribed to a topic in the Google Groups "django-hstore" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-hstore/gvojz9ZSN-o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-hstor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages