我第一次直接使用 Dynamo,在本地的玩具项目中。我正在尝试创建由条件表达式保护的记录 - 如果用户名(范围键)或uniqueId(哈希键)已经存在,则失败:。但是,当我放置具有相同用户名的记录时,我不会遇到冲突 - 并且对表的扫描证实了这一点。我正在使用localstack(本地AWS模拟),如果这有所作为的话。"attribute_not_exists(UserId) and attribute_not_exists(Username)"问题:我应该使用事务还是其他抽象?我是否在表设置中构造了错误的键(请参阅 q 的底部)?是否需要在条件中指定密钥的类型?以下是创建记录的逻辑:userID := GenerateUniqueID()record := UserCredentialsRecord{ UserID: userID, Username: username, Password: base64.StdEncoding.EncodeToString(hashedPassword), Salt: base64.StdEncoding.EncodeToString(salt), Email: email, AccountCreatedTS: time.Now().Unix(),}...input := &dynamodb.PutItemInput{ Item: av, TableName: aws.String(userCredentialsTableName), ConditionExpression: aws.String("attribute_not_exists(UserId) and attribute_not_exists(Username)"),}..._, err = session.PutItem(input)if err != nil { fmt.Println("Got error calling PutItem:", err.Error())}
2 回答

开满天机
TA贡献1786条经验 获得超13个赞
您对条件表达式的作用有误解。attribute_not_exists
这并不意味着该属性不存在于表中的任何项目上,而是该属性不存在于您正在使用的项目上。
因此,操作按设计工作。
如果要确保没有两个项目被添加到具有相同用户名或用户 ID 的表中,则必须使用这两个属性作为表的主键,或者提出一种替代方法来检查重复项。

HUH函数
TA贡献1836条经验 获得超4个赞
在 DynamoDB 中,+ 构成主键。或者,仅当我们没有为表定义时。hash key
range key
hash key
range key
因此,您想要的不能在范围键上完成,因为这不是唯一的。
这里不这样做的要点是 - 不为您扫描表,而是检查您正在使用的记录/项目(来自ddb查询中的哈希键和范围键)。attribute_not_exists
- 2 回答
- 0 关注
- 135 浏览
添加回答
举报
0/150
提交
取消