pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Johnston <polobo@yahoo.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #9519: Allows storing scalar json, but fails when querying
Date: Mon, 10 Mar 2014 14:09:02 -0700 (PDT)
Message-ID: <1394485742473-5795449.post@n5.nabble.com> (raw)
In-Reply-To: <CA+tXr--Z7ap5mcaBVNKOi9NXd2MaZG9FO4t7cVj3cCMZjz+g6g@mail.gmail.com>
References: <20140310150952.398.53642@wrigleys.postgresql.org>
	<531E17E1.9020009@joh.to>
	<CA+tXr--Z7ap5mcaBVNKOi9NXd2MaZG9FO4t7cVj3cCMZjz+g6g@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

alf.kristian wrote
> If the JSON datatype accepts JSON values (not just objects), which I
> supposed is a valid approach, then shouldn't the JSON query operators be
> able to deal with that?
> 
> This is the "unfortunate" part:
> 
> 
> *select data->>'foo' from jtest; => ERROR:  cannot extract element from a
> scalar*
> So either, only accept JSON object (not simple JSON values), or make the
> JSON query operators work with JSON values as well.
> 
> Having to add an extra check constraint here is not obvious, imho.
> 
> For the record, we are working around this problem, through a small hack.
> Some other system is storing this kind of data in our database. We have
> reported a bug with them to fix it.
> 
> This is not a big problem for us, but we love Postgres, so we thought we
> should report this.
> 
> Cheers,
> Alf
> 
> 
> 
> On 10 March 2014 20:52, Marko Tiikkaja &lt;

> marko@

> &gt; wrote:
> 
>> On 3/10/14, 4:09 PM, 

> alf.kristian@

>  wrote:
>>
>>> I think the insert should fail, since '1' is not valid JSON.
>>>
>>
>> It's a valid "JSON value", it's just not an object.  If you only want to
>> store JSON objects in the table, consider using a CHECK constraint.
>>
>>
>> Regards,
>> Marko Tiikkaja
>>

Not really sure how robust you expect the system to be in the face of
polymorphic columns.

SELECT ('["a","b","c"]'::json)->>'not_a_key'  -- ERROR: cannot extract field
from a non-object

The system supposes that, at a structural level, you are dealing with
column-consistent data and so if you ask for something that does not make
sense (i.e., an object key when you have an array or a scalar) it will warn
you.

I guess, in theory, any de-referencing that does not find a valid target
could return NULL...though I'm not sure that is an improvement.

This is a relational database and so it is expected that a column defines a
single thing and that thing naturally can be one-of a scalar, object, or
array.  Given that underlying assumption - though likely never truly spelled
out anywhere in the documentation - allowing and then throwing a run-time
error when the specific sub-type of json does not match the given operator
makes sense.

David J.






--
View this message in context: http://postgresql.1045698.n5.nabble.com/BUG-9519-Allows-storing-scalar-json-but-fails-when-querying-...
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (14+ messages)  latest in thread

Message-ID: <1394485742473-5795449.post@n5.nabble.com>
Permalink:  ../1394485742473-5795449.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1394485742473-5795449.post@n5.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: polobo@yahoo.com
  Subject: Re: BUG #9519: Allows storing scalar json, but fails when querying
  In-Reply-To: <1394485742473-5795449.post@n5.nabble.com>
  References: <20140310150952.398.53642@wrigleys.postgresql.org> <531E17E1.9020009@joh.to> <CA+tXr--Z7ap5mcaBVNKOi9NXd2MaZG9FO4t7cVj3cCMZjz+g6g@mail.gmail.com> <1394485742473-5795449.post@n5.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox