Reputation: 1315
I am trying to insert a single record into a collection with a partition key of /newpart
Just to make sure I'm interpreting what a partition key is, I'm assuming it is the value as shown here.
Within that collection I have a stored procedure called createMyDocument
which contains
function createMyDocument(documentToCreate) {
var context = getContext();
var collection = context.getCollection();
var accepted = collection.createDocument(collection.getSelfLink(),
documentToCreate,
function (err, documentCreated) {
if (err) throw new Error('Error' + err.message);
context.getResponse().setBody(documentCreated.id)
});
if (!accepted) return;
}
I call that via Python with:
config = {
'ENDPOINT': 'deleted',
'MASTERKEY': 'deleted',
'DOCUMENTDB_DATABASE': 'database',
'DOCUMENTDB_COLLECTION': 'newup'
};
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY'] ,'DisableSSLVerification' : 'true' })
data = {"name":"Ben","age":30,"city":"New York"}
client.ExecuteStoredProcedure('dbs/database/colls/newup/sprocs/createMyDocument', data, {"partitionKey" : "/newpart"})
The error returned is
Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted
So I'm confused by the error message because the stored procedure is (to my understanding) stored within the collection with a partition key of /newpart
. That's the same collection, and therefore the same partition key(?) that I am trying to insert the document into.
The error suggests otherwise however.
Upvotes: 0
Views: 1765
Reputation: 23782
When stored procedure is executed from client, RequestOptions specify the partition key, stored procedure will run in context of this partition and cannot operate (e.g. create) on docs that have different partition key value.
What you can do is to execute the stored procedure from client for each partition key. For instance, if stored procedure is to bulk-create documents, you can group the docs by partition key and send each group (can be done in parallel) to the stored procedure providing partition key value in RequestOptions. Would that be helpful?
You don't have to create stored procedure for each partition key, just create once without providing partition key.
According to your code, it seems that you're attempting to insert a document with no partition key and it's going to get grouped in a special partition for documents with no partition key.
In addition, you could refer to this thread: Azure DocumentDB - Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted
Hope it helps you.
Update
Please modify your code as below:
data = {"name":"Ben","age":30,"city":"New York","newpart":"11"}
client.ExecuteStoredProcedure('dbs/db/colls/newcoll/sprocs/createMyDocument', data , {"partitionKey": "11"})
Since your collection has a partitioning key set, the document you want to insert must have the partitioning key set, otherwise the database will not know which partition to place it in.
Of course, the partitioning key which is set in the document should also be consistent with the partitioning key which is set when the stored procedure executes, otherwise you will have the above issue. Because you can only manipulate the partitions you set up when you execute the stored procedure.
Upvotes: 2