UpdateExpression 使用 boto3 在 DynamoDB table 中为更新函数获取动态值
UpdateExpression taking dynamic values for update function in DynamoDB table using boto3
这是第一部分的两部分问题
现在我想要实现的是具有要更新的动态值
def lambda_handler(event, context):
param = event['queryStringParameters']['employeID']
name = event['queryStringParameters']['employeName']
dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
table = dynamodb.Table('api_demo_employe')
column = [cloumn1,cloumn2......]
for i in range(0,len(column):
query = 'SET {} = :f'.format(column[i])
response = table.update_item(
Key = {
'employeID' : param
},
ExpressionAttributeValues = {
':f': name
},
UpdateExpression = query
)
我收到一个错误
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute employeID. This attribute is part of the key",
我知道问题在于我如何处理 UpdateExpression
谁能帮我把这个放好?
您正在尝试更新项目的密钥,但没有成功。您将必须删除并重新创建项目。
详情请参考Amazon DynamoDB documentation:
You cannot use UpdateItem
to update any primary key attributes. Instead, you will need to delete the item, and then use PutItem
to create a new item with new attributes.
这是第一部分的两部分问题
现在我想要实现的是具有要更新的动态值
def lambda_handler(event, context):
param = event['queryStringParameters']['employeID']
name = event['queryStringParameters']['employeName']
dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
table = dynamodb.Table('api_demo_employe')
column = [cloumn1,cloumn2......]
for i in range(0,len(column):
query = 'SET {} = :f'.format(column[i])
response = table.update_item(
Key = {
'employeID' : param
},
ExpressionAttributeValues = {
':f': name
},
UpdateExpression = query
)
我收到一个错误
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: One or more parameter values were invalid: Cannot update attribute employeID. This attribute is part of the key",
我知道问题在于我如何处理 UpdateExpression
谁能帮我把这个放好?
您正在尝试更新项目的密钥,但没有成功。您将必须删除并重新创建项目。
详情请参考Amazon DynamoDB documentation:
You cannot use
UpdateItem
to update any primary key attributes. Instead, you will need to delete the item, and then usePutItem
to create a new item with new attributes.