unique index on embedded json object - Mailing list pgsql-general

From Lee Jason
Subject unique index on embedded json object
Date
Msg-id SNT151-W958134DD9A06AB822BE1AED0B20@phx.gbl
Whole thread Raw
Responses Re: unique index on embedded json object
Re: unique index on embedded json object
List pgsql-general
Hi forks,

I am testing postgresql 9.4 beta2 right now.  I am wondering if it is possible to create a unique index on embedded json object?

For example,

I create a table names "products"

CREATE TABLE products (oid serial primary key, data jsonb)

Now, I try to insert json object into data column.

{"id": "12345",
"bags": [{
    "sku": "abc123",
    "price": 0,
},
{
    "sku": "abc123",
    "price": 0,
}]
}

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.  

I tried to create a unique index like below, but it failed.

CREATE UNIQUE INDEX product_sku_index ON products( (data->'bags'->'sku') )

Any suggestions, please?  Thank you.

Thanks,
Jason

pgsql-general by date:

Previous
From: Eric Smith
Date:
Subject: Re: can't restore database created with pg_dump
Next
From: John R Pierce
Date:
Subject: Re: unique index on embedded json object