MerryFairyTalesAD440 / API

1 stars 2 forks source link

PUT Book is creating books with duplicate Ids instead of updating - Ids must be unique #100

Closed mtolrom closed 5 years ago

mtolrom commented 5 years ago

PUT book should only update the existing book not create a new one with same Id. For example a this PUT call will create another book, not update the existing one : https://melanieoneboxfunctionsprint3.azurewebsites.net/v1/books/d4af5fd4-e83d-43c2-855a-f44ef7ebb504

Fremonster commented 5 years ago

It turns out that with Cosmos DB, the UpsertAsync() method sometimes updates the existing book, but sometimes it creates a new book. Cosmos DB uses the document id, plus the partition key to identity a unique object. The document id alone is not enough. It treats the missing partition key as undefined and Upsert allows for the creation of a new document if Cosmos DB thinks that the one you are uploading is different than the one you are trying to update. This is why it will allow another document with the same id to be added to the database with the new information. If you use ReplaceDocumentAsync() and reference the SelfLink of the document, you ensure that you are updating the exact document. This is what I did to fix this bug.