question

Fred-6278 avatar image
0 Votes"
Fred-6278 asked AnuragSharma-08 commented

Create a document in Stored Procedure raises a Partition Key error

Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted

I'm simply trying to create a document from a stored procedure, here is the sample code:


 function sample(prefix) {
     var collection = getContext().getCollection();
    
     var isAccepted = collection.createDocument(
         collection.getSelfLink(),
         {tests_stored_proc: Date.now()},
         {disableAutomaticIdGeneration: false},
     function (err, item, options) {
         if (err) throw err;
    
         var response = getContext().getResponse();
         var body = { prefix: prefix, item: item };
         response.setBody(JSON.stringify(body));
     });
    
     if (!isAccepted) throw new Error('The query was not accepted by the server.');
 }

I create this stored procedure from Azure Dashboard, and attempted to execute it from there as well. The container was created a long time ago at which point I don't believe partition keys had to be defined. I'm not sure which value I should provide as "Partition key" when I execute the procedure, but none of the ones I've tried have worked.

Beside creating documents, I was hoping to test whether throwing an error would cancel the creation of the documents created within the transaction. I also want to validate whether created documents can be queried within the transaction.

Thank you.

azure-cosmos-db
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi anonymous user-6278, welcome to Microsoft Q&A forum.

In Azure Cosmos DB, stored procedures are scoped to single partition keys and would not work if we don't pass the correct partition key. The error message as reported by you mentions the same.

Can you please go to Data Explorer and try checking if any partition key is set there as highlighted in below screenshot:

87558-image.png

Also please let us know how are you calling this stored procedure to create documents. If through any SDK, just provide few lines of code to check more on this.


0 Votes 0 ·
image.png (39.3 KiB)

Hi AnuragSharma,

As mentioned, the partition key is unknown. The "Partition Key" part of the screen you shared is not displayed for this instance of Cosmos DB, however it is for newer instances.

Regarding the creationg of a document, the code is within the stored procedure.

0 Votes 0 ·

Hi anonymous user-6278, thanks for reverting back.

With the latest configurations of Azure Cosmos DB, stored-procedure would only work within scope of partition keys. As per the article, "Stored procedures are associated with an Azure Cosmos container and stored procedure execution is scoped to a logical partition key. Stored procedures must include a logical partition key value during execution that defines the logical partition for the scope of the transaction."

In the case of non-partitioned container, one way is to first migrate the container to partitioned container and then we can run a stored procedure against it.

Migrate non-partitioned containers to partitioned containers

Please let me know if this helps.

0 Votes 0 ·

0 Answers