Re: BUG #9519: Allows storing scalar json, but fails when querying

Поиск
Список
Период
Сортировка
От Jeff Janes
Тема Re: BUG #9519: Allows storing scalar json, but fails when querying
Дата
Msg-id CAMkU=1xiHuGMBnDTo48D4L5J8sZErkPVCiF7J=yA-joPAx=EBA@mail.gmail.com
обсуждение исходный текст
Ответ на BUG #9519: Allows storing scalar json, but fails when querying  (alf.kristian@gmail.com)
Ответы Re: BUG #9519: Allows storing scalar json, but fails when querying  (Alf Kristian Støyle <alf.kristian@gmail.com>)
Список pgsql-bugs
On Mon, Mar 10, 2014 at 8:09 AM, <alf.kristian@gmail.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      9519
> Logged by:          Alf Kristian St=F8yle
> Email address:      alf.kristian@gmail.com
> PostgreSQL version: 9.3.2
> Operating system:   Red Hat 4.6.3-2
> Description:
>
> Steps to reproduce:
> create table jtest (data json);
> =3D> CREATE TABLE
>
> insert into jtest (data) values ('1');
> =3D> INSERT 0 1
>
> select data->>'foo' from jtest;
> =3D> ERROR:  cannot extract element from a scalar
>
>
> I think the insert should fail, since '1' is not valid JSON.
>
> After the data is in the database every query using the ->> operator,
> hitting the row containing '1' will fail.
>

Lets say the value was instead {"a":1}.

Now every query using data->'a'->>'b' will fail when it hits that row.

So forbidding values does not fix the problem, it just moves it down a
level.

A possible solution is to make ->> return NULL (like it does for accessing
values of non-existent keys) rather than raise an error when used on a
scalar.  Whether this would be an improvement, I don't know.

Note that the construct:
data #> '{a,b}'
does return null in this case, and does not raise an error.  You could
argue that that is an inconsistency.  On the other hand, you could argue it
provides you with the flexibility to accomplish different things depending
on which you desire.

So if you want the NULL behavior, you could use this to get it:

data #>> '{foo}'


Cheers,

Jeff

В списке pgsql-bugs по дате отправления:

Предыдущее
От: David Johnston
Дата:
Сообщение: Re: BUG #9519: Allows storing scalar json, but fails when querying
Следующее
От: Christian Kruse
Дата:
Сообщение: Re: BUG #9519: Allows storing scalar json, but fails when querying