2
votes

I'm trying to update an existing row in an Azure Table from my Azure Function but it errors with:

Functions.HttpTrigger1. Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. Microsoft.Azure.WebJobs.Extensions.Storage: The specified entity already exists.

Some research seems to indicate that you need to specify an ETag : '*', but I have been unsuccessful with this (I'm probably not using it correctly). There is a C# sample here (linked from the referenced git issue). Some further research seems to indicate that the ETag value needs to be part of the header, but I cannot confirm this, nor if its true, did I see where/how I can pass headers.

Below I'm using the 'owner' as the RowKey, wanting to update the 'val2Update' on a new trigger.

Py Code

def main(req: func.HttpRequest, functionTableStorage: func.Out[str], messageJSON) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    owner = req.params.get('owner')
    val2Update = req.params.get('val')

    if owner:
        data = {
            "PartitionKey": "message",
            "RowKey": owner,
            "tester" : val2Update,
            "ETag": "*"
        } 
        functionTableStorage.set(json.dumps(data))
        
        return func.HttpResponse(f"Thanks, {owner}.")

Bindings

{
  "type": "table",
  "direction": "out",
  "name": "functionTableStorage",
  "tableName": "masterTable",
  "connection": "AzureWebJobsStorage"
},
1
Since you want to update, you should not use output binding. Please put the logic in the body of your function. You can have a look of my answer.:)Cindy Pau

1 Answers

1
votes

Since you want to update the Azure Table with Function App, you should not use the output binding.

If I understand correctly about what you want, you should put the basic logic in the body of your function trigger like this:(Before debug, you should first run this command: pip install azure-cosmosdb-table)

import logging

import azure.functions as func
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    table_service = TableService(connection_string='DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net')
    task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
        'description': 'Take out the garbage', 'priority': 250}
    table_service.update_entity('tasktable', task)
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

This is my original entity:

enter image description here

And this is the entity after update:

enter image description here

This is the offcial document:

https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-python?toc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fstorage%2Ftables%2Ftoc.json&bc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fbread%2Ftoc.json#update-an-entity