python 在 dynamodb 中使用全局二级索引时出错
Error while using Global Secondary index in dynamodb in python
我在 AWS dynamodb 中创建了一个全局二级索引,其中在用户 table 中,我在其中使用 phone 作为 GSI。我收到错误消息:
'Table UsersTable has no index: PHONE_INDEX'.
下面是用户 table 和 phone 作为 GSI 的代码片段。我无法弄清楚出现此错误的原因。
from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex, KeysOnlyProjection, AllProjection
from pynamodb.attributes import( UnicodeAttribute, NumberAttribute)
from passlib.hash import pbkdf2_sha256 as sha256
import auth.exceptions as exception
class PhoneIndex(GlobalSecondaryIndex):
class Meta:
index_name = 'PHONE_INDEX'
read_capacity_units = 2
write_capacity_units = 2
# All attributes are projected
projection = KeysOnlyProjection()
# This attribute is the hash key for the index
# Note that this attribute must also exist
# in the model
phone = UnicodeAttribute(hash_key=True)
class UserModel(Model):
# Table Users
if config.IS_OFFLINE:
class Meta:
table_name = config.USERS_TABLE
index_name = 'PHONE_INDEX'
host = "http://localhost:8000"
region = config.REGION
aws_access_key_id = 'my_access_key_id'
aws_secret_access_key = 'my_secret_access_key'
aws_session_token = 'my_session_token'
else:
class Meta:
table_name = config.USERS_TABLE
region = config.REGION
customerid= UnicodeAttribute()
phone = UnicodeAttribute()
name = UnicodeAttribute()
username = UnicodeAttribute(hash_key=True)
password= UnicodeAttribute()
phone_index=PhoneIndex()
我收到此错误是因为我没有在我的 cloudformation 模板文件中添加全局二级索引。
我在 AWS dynamodb 中创建了一个全局二级索引,其中在用户 table 中,我在其中使用 phone 作为 GSI。我收到错误消息:
'Table UsersTable has no index: PHONE_INDEX'.
下面是用户 table 和 phone 作为 GSI 的代码片段。我无法弄清楚出现此错误的原因。
from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex, KeysOnlyProjection, AllProjection
from pynamodb.attributes import( UnicodeAttribute, NumberAttribute)
from passlib.hash import pbkdf2_sha256 as sha256
import auth.exceptions as exception
class PhoneIndex(GlobalSecondaryIndex):
class Meta:
index_name = 'PHONE_INDEX'
read_capacity_units = 2
write_capacity_units = 2
# All attributes are projected
projection = KeysOnlyProjection()
# This attribute is the hash key for the index
# Note that this attribute must also exist
# in the model
phone = UnicodeAttribute(hash_key=True)
class UserModel(Model):
# Table Users
if config.IS_OFFLINE:
class Meta:
table_name = config.USERS_TABLE
index_name = 'PHONE_INDEX'
host = "http://localhost:8000"
region = config.REGION
aws_access_key_id = 'my_access_key_id'
aws_secret_access_key = 'my_secret_access_key'
aws_session_token = 'my_session_token'
else:
class Meta:
table_name = config.USERS_TABLE
region = config.REGION
customerid= UnicodeAttribute()
phone = UnicodeAttribute()
name = UnicodeAttribute()
username = UnicodeAttribute(hash_key=True)
password= UnicodeAttribute()
phone_index=PhoneIndex()
我收到此错误是因为我没有在我的 cloudformation 模板文件中添加全局二级索引。