Re: unique index on embedded json object

From: Peter Geoghegan <peter(dot)geoghegan86(at)gmail(dot)com>
To: Lee Jason <jawc(at)hotmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: unique index on embedded json object
Date: 2014-09-22 04:03:06
Message-ID: CAEYLb_V75FD+NzAJFBqYGceYs=vL=dcpaH_NnTaM89vBafCeQA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Sep 21, 2014 at 5:23 AM, Lee Jason <jawc(at)hotmail(dot)com> wrote:
> {"id": "12345",
> "bags": [{
> "sku": "abc123",
> "price": 0,
> },
> {
> "sku": "abc123",
> "price": 0,
> }]
> }

That's invalid JSON - there are stray commas.

> However, I want sku of bags to be unique. It means the json can't be
> inserted into products tables because sku is not unique in this case.

Unique indexes are useful for ensuring that each row is unique based
on some criteria, possibly a quite complex expression. However, it's
not clear what behavior is expected here, since multiple SKUs may
appear per row. If you had a table with "products", with a jsonb
column, and one row per product, you could then usefully extract at
most one SKU per row, and that could work fine. But the structure you
present isn't amenable to that.

--
Regards,
Peter Geoghegan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message smcg2297 2014-09-22 04:16:06 Re: a couple questions about convert()
Previous Message John R Pierce 2014-09-22 03:58:49 Re: unique index on embedded json object