PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

GlobalSecondaryIndex + Polymorphism

Open ynie opened this issue 2 years ago • 1 comments

Hey all, I'm new to pynamoDB. Here is my setup

class DateIndex(GlobalSecondaryIndex):
    class Meta:
        projection = AllProjection()
    data_type = UnicodeAttribute(hash_key=True)

class CredentialBase(Model):
    id = UnicodeAttribute(hash_key=True)
    date_index = DataTypeDateIndex()
    cls = DiscriminatorAttribute()

class Credential(CredentialBase, discriminator='Credential"):
    class Meta:
        table_name = ""

Why wouldn't this work? results = Credential.date_index.query("", limit=1)

It tries to look up the dateIndex on CredentialBase instead of Credential. I can fix the problem by moving the date_index to the childClass Credential. Is that the correct way of doing it? Thanks!

ynie avatar Aug 05 '23 04:08 ynie

For anyone else trying to find a solution to this issue, another workaround is to use index_name on the model query, rather than querying the index instance.

i.e. instead of:

results = Credential.date_index.query("", limit=1)

which returns CredentialBase instances

try:

results = Credential.query("", index_name="my_index_name",  limit=1)

which returns Credential instances

dpretty avatar Jul 09 '24 09:07 dpretty