I'm testing Postgresql 9.4 beta2 right now. I am wondering if it is possible to create a unique index on a json inline object?
I create a table name products:
CREATE TABLE products (oid serial primary key, data jsonb)
Now I am trying to insert a json object into a data column.
{
"id": "12345",
"bags": [
{
"sku": "abc123",
"price": 0,
},
{
"sku": "abc123",
"price": 0,
}
]
}
However, I want the skubags to be unique. This means that json cannot be inserted into product tables because it is skunot unique in this case.
I tried to create a unique index as shown below, but this failed.
CREATE UNIQUE INDEX product_sku_index ON products( (data->'bags'->'sku') )
Any suggestions?
source
share