Azure / Azure/azure-sdk-for-python

[Bug] replace_container fails on containers with uniqueKeyPolicy

Open
#40,070 5 comments 0 reactions 1 assignee Claimed by @allenkim0129 View on GitHub
bug Client Cosmos customer-reported needs-team-attention Service Attention
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

- **Package Name**: azure-cosmos
- **Package Version**: 4.9.0
- **Operating System**: MacOS 15.3.2
- **Python Version**: 3.13.2

**Describe the bug**
replace_container always fails on containers that have uniqueKeyPolicy defined on them, even though there is no intention of modifying the uniqueKeyPolicy.

**To Reproduce**
Steps to reproduce the behavior:
1. Create a container with a uniqueKeyPolicy using any method.
2. Connect to the database, and obtain a `DatabaseProxy` object using `db = get_database_client('db_name')`
3. Get the container client for the container created in step 1 using `container = db.get_container_client('container_name')`
4. Try to modify the indexing policy on the container using `db.replace_container(container=container, partition_key=container.read()['partitionKey'], indexing_policy=indexing_policy)` with any non-empty indexing policy object.

The operation fails with:

```
azure.cosmos.exceptions.CosmosHttpResponseError: (Forbidden) Message: {"Errors":["The unique index cannot be modified. To change the unique index, remove the collection and re-create a new one."]}
```

even though the unique index was not modified.

**Expected behavior**
The operation succeeds, the indexes contained in the indexingPolicy are modified to the `indexing_policy` object passed in step 4, the unique indexes stay the same as before.

**Screenshots**
N/A

**Additional context**
I know the documentation mentions the unspecified parameters will be reset to the their default values, however the unique_key_policy parameter does not exist on the replace_container operation, which makes sense, as it cannot be modified. Keeping it as it is would be the desirable behaviour in this case, especially that the official guide does not present any alternative to update the indexes using an SDK. The operation can be performed rather easily using Azure Portal following the same guide:
https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-indexing-policy?tabs=dotnetv3%2Cpythonv3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.