pynamodb / PynamoDB

A pythonic interface to Amazon's DynamoDB
http://pynamodb.readthedocs.io
MIT License
2.43k stars 426 forks source link

GlobalSecondaryIndex + Polymorphism #1192

Open ynie opened 1 year ago

ynie commented 1 year ago

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!

dpretty commented 1 month ago

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