Skip to content

Unique Key Policy is not being enforced #14214

Description

@donaldduy-lb
  • package: @azure/cosmos
  • version: ^3.6.2
  • Linux:
  • nodejs
    • version: ^8.5.4
  • Google Chrome
    • version/name: 88.0.4324.190 (Official Build) (64-bit)
  • typescript
    • version: ^3.7.3

Describe the bug
The container I have created is not following the unique key policy that I have set. I suspect this is due to the fact that both items are being persisted at the exact same time.

To Reproduce
Steps to reproduce the behavior:

  1. Insert two items that have the exact same _ts, subject and seq value.
  2. Both items persist in the database.

Expected behavior
I expect one or both of these documents to get rejected from persistence.

Screenshots
db1
db2

Additional context
Here is the code I used to create the container and set the unique key policy:

await db.containers.createIfNotExists({
  id: eventstore,     
  partitionKey: { paths: ["/subject"] },    
  uniqueKeyPolicy: {     
     uniqueKeys: [   
         { paths: ["/seq"] }   
     ]    
  }   
})

I am also using a stored procedure to insert items into this database:

function bulkInsertItems(items) {
  var container = getContext().getCollection();
  var containerLink = container.getSelfLink();

  // Validate input
  if (!items) throw new Error("The array is undefined or null.");

  // End if there are no items
  if (items.length == 0) {
    getContext().getResponse().setBody(0);
  }

  items.map(item => {
    var isAccepted = container.createDocument(containerLink, item);

    // If the item fails to persist, throw an error and CosmosDB will handle rolling back
    if (!isAccepted) {
      throw new Error("Unable to create item");
    }
  })

  getContext().getResponse().setBody(items.length);
}

Metadata

Metadata

Labels

ClientThis issue points to a problem in the data-plane of the library.Cosmoscustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions