本地二级索引的 PynamoDB 错误:ValueError('Table table_4 has no index: key1_index',)
PynamoDB Error for Local Secondary Index: ValueError('Table table_4 has no index: key1_index',)
我是 pynamodb 的新手,我正在尝试查询 LSI(已在 dynamodb main table 中定义)并得到以下异常。
'exception': ValueError('Table table_4 has no index: key1_index',)
用例:我只想在 hash_key 上查询 LSI,因为我已经为 key1 (range_key) 提供了默认值,并希望根据 key1 获取排序后的数据。
大规模集成电路型号
class LSI_Index(LocalSecondaryIndex):
id = UnicodeAttribute(hash_key=True)
key1 = NumberAttribute(default=int(time.time()), range_key=True)
class Meta:
index = "key1-Index"
projection = IncludeProjection(['id', 'key1', 'key2'])
主要Table型号
class MainModel(Model):
id = UnicodeAttribute(hash_key=True)
key2 = UnicodeAttribute(range_key=True, default='')
key1 = NumberAttribute(default=int(time.time()))
key1_index = LSI_Index() # LSI
class Meta:
.....
存储库方法代码
def read_by_LSI(cls, id):
try:
data = []
iterator = MainModel.key1_index.query(hash_key=id,limit=1,scan_index_forward=False, consistent_read=True)
for item in iterator:
data.append(json.loads(json_dumps(item)))
return data
DynamoDB 主Table描述:
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "key1",
"AttributeType": "N"
}
],
"TableName": "table_4",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": 1647447620.911,
"ProvisionedThroughput": {
"LastIncreaseDateTime": 0.0,
"LastDecreaseDateTime": 0.0,
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 779,
"ItemCount": 7,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table_4",
"LocalSecondaryIndexes": [
{
"IndexName": "key1-Index",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "key1",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "INCLUDE",
"NonKeyAttributes": [
"key2",
"key1",
]
},
"IndexSizeBytes": 779,
"ItemCount": 7,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table_4/index/key1-Index"
}
]
}
}
请告诉我哪里做错了,因为我无法调试问题。
提前致谢。!!
所以基本上,在 pynamodb 中定义 LSI 模型时,请确保 Meta class 属性名称与文档中提到的相同 - https://pynamodb.readthedocs.io/en/latest/indexes.html
在上面的例子中,我犯了以下错误,导致出现 ValueError('table table_4 doesn't have key1_index') Exception.
在 LSI_Model Class 的 MetaClass 中,我使用了错误的属性名称 'index' 而不是 'index_name'。我已经更新了代码,现在可以正常工作了。
class LSI_Index(LocalSecondaryIndex):
id = UnicodeAttribute(hash_key=True)
key1 = NumberAttribute(default=int(time.time()), range_key=True)
class Meta:
index_name = "key1-Index"
projection = IncludeProjection(['id', 'key1', 'key2'])
注:
LSI Meta Class 属性应命名如下(文档中提到):
- index_name
- 投影
等等..
我是 pynamodb 的新手,我正在尝试查询 LSI(已在 dynamodb main table 中定义)并得到以下异常。
'exception': ValueError('Table table_4 has no index: key1_index',)
用例:我只想在 hash_key 上查询 LSI,因为我已经为 key1 (range_key) 提供了默认值,并希望根据 key1 获取排序后的数据。
大规模集成电路型号
class LSI_Index(LocalSecondaryIndex):
id = UnicodeAttribute(hash_key=True)
key1 = NumberAttribute(default=int(time.time()), range_key=True)
class Meta:
index = "key1-Index"
projection = IncludeProjection(['id', 'key1', 'key2'])
主要Table型号
class MainModel(Model):
id = UnicodeAttribute(hash_key=True)
key2 = UnicodeAttribute(range_key=True, default='')
key1 = NumberAttribute(default=int(time.time()))
key1_index = LSI_Index() # LSI
class Meta:
.....
存储库方法代码
def read_by_LSI(cls, id):
try:
data = []
iterator = MainModel.key1_index.query(hash_key=id,limit=1,scan_index_forward=False, consistent_read=True)
for item in iterator:
data.append(json.loads(json_dumps(item)))
return data
DynamoDB 主Table描述:
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "key1",
"AttributeType": "N"
}
],
"TableName": "table_4",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": 1647447620.911,
"ProvisionedThroughput": {
"LastIncreaseDateTime": 0.0,
"LastDecreaseDateTime": 0.0,
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 779,
"ItemCount": 7,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table_4",
"LocalSecondaryIndexes": [
{
"IndexName": "key1-Index",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "key1",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "INCLUDE",
"NonKeyAttributes": [
"key2",
"key1",
]
},
"IndexSizeBytes": 779,
"ItemCount": 7,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table_4/index/key1-Index"
}
]
}
}
请告诉我哪里做错了,因为我无法调试问题。 提前致谢。!!
所以基本上,在 pynamodb 中定义 LSI 模型时,请确保 Meta class 属性名称与文档中提到的相同 - https://pynamodb.readthedocs.io/en/latest/indexes.html
在上面的例子中,我犯了以下错误,导致出现 ValueError('table table_4 doesn't have key1_index') Exception.
在 LSI_Model Class 的 MetaClass 中,我使用了错误的属性名称 'index' 而不是 'index_name'。我已经更新了代码,现在可以正常工作了。
class LSI_Index(LocalSecondaryIndex):
id = UnicodeAttribute(hash_key=True)
key1 = NumberAttribute(default=int(time.time()), range_key=True)
class Meta:
index_name = "key1-Index"
projection = IncludeProjection(['id', 'key1', 'key2'])
注: LSI Meta Class 属性应命名如下(文档中提到):
- index_name
- 投影 等等..