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:
- Insert two items that have the exact same _ts, subject and seq value.
- Both items persist in the database.
Expected behavior I expect one or both of these documents to get rejected from persistence.
Screenshots
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:
- Created 3 years ago
- Comments:18 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Let me try to recreate this on my end
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.