【问题标题】:Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST无效的 UpdateExpression:运算符或函数的操作数类型不正确;运算符:ADD,操作数类型:LIST
【发布时间】:2020-05-09 21:46:12
【问题描述】:

我尝试在 UpdateExpression 中使用 ADD 将电子邮件字符串添加到字符串集中,代码如下,但遇到了这个异常: Invalid UpdateExpression: Incorrect operand type for operator or function;运算符:ADD,操作数类型:LIST。

我认为关键是我需要一种将类型更改为 "string set" 的方法,但不确定实现此目的的语义是什么。

        response = wishesTable.update_item(
            Key={
                'title': wishTitle,
                'userMail': wishUsermail
            },
            UpdateExpression='ADD whoLikeList :my_value',
            ExpressionAttributeValues={
                ":my_value": [userEmail]
            },
            ReturnValues="UPDATED_NEW"
        )

【问题讨论】:

    标签: amazon-dynamodb


    【解决方案1】:

    通过引用 TypeSerializer 类(http://boto3.readthedocs.io/en/latest/_modules/boto3/dynamodb/types.html )

    将代码更改为以下代码,将被视为设置:

            response = wishesTable.update_item(
                Key={
                    'title': wishTitle,
                    'userMail': wishUsermail
                },
                UpdateExpression='ADD whoLikeList :my_value',
                ExpressionAttributeValues={
                    ":my_value": set([userEmail])
                },
                ReturnValues="UPDATED_NEW"
            )
    

    【讨论】:

    • 另外请注意,您需要确保该字段最初是使用 set(list) 而不是仅使用 list 设置的,否则它将无法更新它,因为 dynamo 中的属性不会是一个集合.更新为使用 set() 也为我修复了它(谢谢!),只需要更改我的初始插入代码。
    • 谢谢,这为我修好了!仅供参考,您也可以使用集合文字。 ":my_value": {userEmail} 而不是 ":my_value": set([userEmail])
    猜你喜欢
    • 2021-09-25
    • 2021-05-17
    • 1970-01-01
    • 2017-01-24
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多