PynamoDB GSI is not returning any data

42 Views Asked by At

I have a User model in pynamodb with user_id as primary key. There is another attribute username which is a GSI and when queried on it, its not returning any data.

Following the guide - https://pynamodb.readthedocs.io/en/stable/indexes.html

I have a DDB table with GSI defined on it. GSI

  • name: 'UsernameIndex'
  • hash_key: 'Username'
  • projection: 'Keys only'

Versions

  • Python - 3.9
  • boto3-1.34.11
  • Django-4.2.8
  • pynamodb-5.5.1
class UsernameIndex(GlobalSecondaryIndex):
    class Meta:
        index_name = "UsernameIndex" # this name should match with index name on DDB
        region = AWS_REGION
        read_capacity_units = 2
        write_capacity_units = 1
        projection = KeysOnlyProjection()

    username = UnicodeAttribute(hash_key=True, attr_name='Username')


class UserModel(Model):
    
    class Meta:
        table_name = 'RM-User'
        # AWS profile has already been set in settings
        region = AWS_REGION

    user_id = UnicodeAttribute(hash_key=True, attr_name='UserId')
    username = UnicodeAttribute(attr_name='Username')
    password = UnicodeAttribute(attr_name='Password')
    name = Name(attr_name='Name')
    
    # gsi
    username_index = UsernameIndex()

And here is my query

username = 'ABCD'
query_result = UserModel.username_index.query(username)

This query is returning ResultIterator with total_count always set to zero, while i have 5 entries in the DDB with username ABCD.

Note:

  • This UserModel is working fine as I have checked with UserModel.get(hash_key=user_id)

While debugging on vscode I checked the value of user.username_index.username and it was set to a traceback object, which i feel is not correct and i dont have any idea on how to debug it

'Traceback (most recent call last):
File "/Users/pratik/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_resolver.py", line 189, in _get_py_dictionary
    
attr = getattr(var, name)\n  File "/Users/pratik/github/RemindMe-WS/RemindMeDjangoBackend/.env/lib/python3.9/site-packages/pynamodb/attributes.py", line 113, in __get__

attr_name = instance._dynamo_to_python_attrs.get(self.attr_name, self.attr_name)\nAttributeError: \'UsernameIndex\' object has no attribute \'_dynamo_to_python_attrs\'\n'

I have also tried using UsernameIndex.Meta.table_name='UsernameIndex' as mentioned here, but its not working.

0

There are 0 best solutions below