question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique Key Policy is not being enforced

See original GitHub issue
  • 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);
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
donaldduy-lbcommented, Apr 8, 2021

Let me try to recreate this on my end

1reaction
zfostercommented, Apr 6, 2021

Sounds good, thanks for the info. We’ve had a few asks like I said for Transactional batch so I can see that coming soon. I’m still planning to find time to verify I can reproduce this and would then hand it to the backend team.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to enforce unique keys on CosmosDB using ...
I'm unable to see the unique key policy in the settings so my only way to was to add a console.log before the...
Read more >
Unique key constraints in Azure Cosmos DB - Microsoft Learn
Sparse unique keys are not supported. If some unique path values are missing, they're treated as null values, which take part in the...
Read more >
Constraints in SQL Server: SQL NOT NULL, UNIQUE and SQL ...
This article explains the useful constraints SQL NOT NULL, Unique and Primary Key constraints in SQL Server examples with user cases.
Read more >
Resolve KMS key policy error "Policy contains a statement ...
The AWS KMS key policy doesn't contain the Amazon Resource Name (ARN), and it contains a principal with a unique ID that is...
Read more >
How can we enforce [Unique, Primary Key, Foreign Key (UPF ...
I'm not aware of any high-performance big data / OLAP system that enforces UPK constraints because OLAP systems are MPP (Massively Parallel Processing)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found