[Python] Python CosmosDB upsert_item deletes document

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I am going to update a document using upsert_item function of CosmosDB Python SDK.

    Here is a script:

    from dotenv import load_dotenv
    from azure.cosmos import CosmosClient
    import os
    import uuid

    def update():
    load_dotenv()
    # Initialize the Cosmos DB client
    endpoint = os.getenv("COSMOSDB_ENDPOINT")
    key = os.getenv("COSMOSDB_KEY")
    client = CosmosClient(endpoint, key)

    # Specify your database and container (collection) names
    database_name = os.getenv("COSMOSDB_DB")
    container_name = os.getenv("COSMOSDB_CONTAINER")

    # Retrieve an item by its ID
    container = client.get_database_client(database_name).get_container_client(container_name)

    id = str(uuid.uuid4())
    url = "https://google.com/"

    container.create_item(
    {
    "id": id,
    "status": "pending", # => started, analyzing, finished
    "url": url,
    "categories": ["7149b375-8cb2-4180-ae03-27fd0da409d0"],
    "doctype": "url"
    }
    )

    query_text = f"SELECT * FROM c WHERE c.id='{id}'"
    query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
    data = list(query_items_response)
    print(len(data)) # returns 1

    for item in data:
    print(f"Updating {item['url']}")
    item["categories"] = []
    item["doctype"] = "pdfDocument"
    container.upsert_item(item)

    updated_query_items_response = container.query_items(query=query_text, enable_cross_partition_query=True)
    updated_data = list(updated_query_items_response)
    print(updated_data)
    print(len(updated_data)) # returns 0
    # Confirm created data using CosmosDB data explorer.

    if __name__ == "__main__":
    update()


    After creating a document, I updated it using the upsert_item method. Sometimes, when I query the data by its ID, I can see the document.

    However, in reality, it has been removed. Even after waiting for some time, I am unable to retrieve the document, both through the code and in the Cosmos DB Data Explorer.

    And I found a strange problem. If I change item["doctype"] = "pdfDocument" to item["doctype"] = "site" or others, it works as expected.

    It is quite strange.

    Continue reading...

Compartilhe esta Página