user2133404
user2133404

Reputation: 1857

Azure cosmos db collection is not getting the partition key

I am trying to create a azure cosmos database and collection in my c# code.

await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = "data"});
DocumentCollection dCollection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("data"), new DocumentCollection { Id = "coll"}, new RequestOptions { OfferThroughput = 400, , PartitionKey = new PartitionKey("/id") });
// dashboardCollection.PartitionKey.Paths.Add("/id");

When I go to portal.azure.com and check my document DB, the collection is created. When I go to Scale and Settings for the collection, I don't see a partition key.

I created another collection manually and it shows the partition key in the Scale and Settings section.

The delete function is throwing an error because of this partition key error

Inserted a record with id 1 successfully into the document DB. The following delete fails saying that the partitionKey is invalid.

ResourceResponse<Document> response = await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri("data", "coll", "1"), new RequestOptions { PartitionKey = new PartitionKey("1") });

Upvotes: 1

Views: 2989

Answers (1)

Krishnan Sundaram
Krishnan Sundaram

Reputation: 1401

I am from the CosmosDB engineering team.

When creating a DocumentCollection, please ensure that the partition key is provided in the DocumentCollection object, like so:

PartitionKeyDefinition pkDefn = new PartitionKeyDefinition() { Paths = new Collection<string>() { "/id" } };
DocumentCollection dCollection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("data"), new DocumentCollection { Id = "coll", PartitionKey = pkDefn }, new RequestOptions { OfferThroughput = 400, PartitionKey = new PartitionKey("/id") });

The PartitionKey on the RequestOptions is not honored during collection CRUD requests, since we expect the PartitionKey to be part of the collection object. The PartitionKey on the RequestOptions is honored during document CRUD requests.

Upvotes: 3

Related Questions