【发布时间】:2021-06-26 20:36:06
【问题描述】:
这是我的表 seller 的 dynamoose 架构
const schema = new dynamoose.Schema({
PK: {
type: String, //ni letak emel.toLowerCase() + #main/business/delivery/ehailing
hashKey: true,
},
SK: {
type: String,
rangeKey: true,
"index": { //utk 'auto' display kedai bila user ada kat location tu
"name": "SKIndex",
"global": true,
"rangeKey": "location"
}
},
"location": String,
}, {
"saveUnknown": true,
"timestamps": true
});
如您在上面看到的,我创建了一个 GSI,其中 SK 作为名为 SKIndex 的 hashkey,并以 location 作为 rangeKey。所以我尝试执行下面的查询
var SKIndex_search = "some value"
var locality = "some value too"
var filter = new dynamoose.Condition().where("SKIndex").eq(SKIndex_search).filter("location").beginsWith(locality);
var getResult = await Seller.query(filter).exec()
但它总是会返回错误"InvalidParameter: Index can't be found for query."
===============
运行此查询时Seller.query(SKIndex_search).using("SKIndex").filter("location").beginsWith(locality).exec()
会显示错误信息ValidationException: Query condition missed key schema element
完整的错误日志:
aws:dynamodb:describeTable:response - {
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "PK",
"AttributeType": "S"
},
{
"AttributeName": "SK",
"AttributeType": "S"
},
{
"AttributeName": "location",
"AttributeType": "S"
}
],
"TableName": "earthlings_seller",
"KeySchema": [
{
"AttributeName": "PK",
"KeyType": "HASH"
},
{
"AttributeName": "SK",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2021-06-26T20:50:13.233Z",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
"LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"TableSizeBytes": 312,
"ItemCount": 1,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller",
"GlobalSecondaryIndexes": [
{
"IndexName": "SKIndex",
"KeySchema": [
{
"AttributeName": "SK",
"KeyType": "HASH"
},
{
"AttributeName": "location",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"IndexSizeBytes": 312,
"ItemCount": 1,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/earthlings_seller/index/SKIndex"
}
]
}
}
aws:dynamodb:query:request - {
"ExpressionAttributeNames": {
"#qra": "location"
},
"ExpressionAttributeValues": {
":qrv": {
"S": "nilai"
}
},
"TableName": "earthlings_seller",
"IndexName": "SKIndex",
"KeyConditionExpression": "begins_with (#qra, :qrv)"
}
【问题讨论】:
-
where接受属性名称 (SK) 而不是索引名称 (SKIndex)。您可以使用using方法设置索引名称。但是,如果您将条件更改为使用where("SK"),它应该可以工作。 -
将其更改为
where("SK")有效,谢谢!所以在幕后,dynamoose/dynamodb 将尝试自行确定查询中的给定属性名称是“主”索引还是全局索引?无论如何,我也尝试了using和Seller.query(SKIndex_search).using("SKIndex").filter("location").beginsWith(locality).exec()的方法,但我会收到这个错误ValidationException: Query condition missed key schema element -
SKIndex_search设置为什么? -
@CharlieFish
SKIndex_search只是用户提供的一个值,在这种情况下它是用户的国家代码,例如,my,au -
哦,从来不知道 Dynamoose 有自己的 Slack 频道。到时我会在那里见你。谢谢!