![](/img/trans.png)
[英]The number of conditions on the keys is invalid - Error in dynamo item update using DynamoDBMapper
[英]dynamodb query: ValidationException: The number of conditions on the keys is invalid
我有以下模式,我基本上只是試圖讓一個表以id
作為主鍵,並且code
和secondCode
都是用於查詢表的全局二級索引。
resource "aws_dynamodb_table" "myDb" {
name = "myTable"
billing_mode = "PAY_PER_REQUEST"
hash_key = "id"
attribute {
name = "id"
type = "S"
}
attribute {
name = "code"
type = "S"
}
attribute {
name = "secondCode"
type = "S"
}
global_secondary_index {
name = "code-index"
hash_key = "code"
projection_type = "ALL"
}
global_secondary_index {
name = "second_code-index"
hash_key = "secondCode"
projection_type = "ALL"
}
}
當我嘗試通過code
查找一項時
const toGet = Object.assign(new Item(), {
code: 'code_456',
});
item = await dataMapper.get<Item>(toGet);
在當地我得到
ValidationException: The number of conditions on the keys is invalid
在我得到的數據庫的已部署實例上
The provided key element does not match the schema
我可以從日志中看到key
未被填充
Serverless: [AWS dynamodb 400 0.082s 0 retries] getItem({ TableName: 'myTable', Key: {} })
這是Item
的 class 配置
@table(getEnv('MY_TABLE'))
export class Item {
@hashKey({ type: 'String' })
id: string;
@attribute({
indexKeyConfigurations: { 'code-index': 'HASH' },
type: 'String',
})
code: string;
@attribute({
indexKeyConfigurations: { 'second_code-index': 'HASH' },
type: 'String',
})
secondCode: string;
@attribute({ memberType: embed(NestedItem) })
nestedItems?: Array<NestedItem>;
}
class NestedItem {
@attribute()
name: string;
@attribute()
price: number;
}
您面臨的問題是由於在索引上調用GetItem
,這是不可能的。 GetItem
必須以單個項目為目標,索引可以包含具有相同鍵的多個項目(與基表不同),因此您只能在索引上使用多項目 API,即Query
和Scan
。
我查看了您為 package 鏈接的回購協議,我認為您需要使用帶有indexName
參數的.query(...)
方法來告訴 DynamoDB 您想要使用該二級索引。 通常在 DynamoDB 中, get
操作使用默認鍵(在您的情況下,您將使用get
查詢id
,並使用query
查詢索引)。
檢查文檔,它不是很清楚 - 如果您查看GetItem
參考,您會發現沒有地方可以提供索引名稱來實際使用索引,而Query
操作允許您提供一個。 至於為什么要這樣查詢,可以看這個: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.