pynamodb / PynamoDB

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

Discriminator, Index with KeysOnlyProjection results in query error #1178

Open hookercookerman opened 1 year ago

hookercookerman commented 1 year ago

class ItemEmailIdIndex(GlobalSecondaryIndex):
    class Meta:
        index_name = "itemEmailIdIndex"
        projection = KeysOnlyProjection()

    email_id = UnicodeAttribute(hash_key=True)
    sk = UnicodeAttribute(range_key=True)

class Item(Model, discriminator="Item"):
    class Meta:
        table_name = f"items-{STAGE}"
        region = os.environ.get("AWS_REGION")

    pk = UnicodeAttribute(hash_key=True)
    sk = UnicodeAttribute(range_key=True)
    itemEmailIdIndex = ItemEmailIdIndex()
    cls = DiscriminatorAttribute()

cls.itemEmailIdIndex.query(email_id, limit=1)
DEBUG:pynamodb.connection.base:Calling Query with arguments {'TableName': 'items-hookercookerman', 'KeyConditionExpression': '#0 = :0', 'FilterExpression': '#1 IN (:1)', 'IndexName': 'itemEmailIdIndex', 'Limit': 1, 'ExpressionAttributeNames': {'#0': 'email_id', '#1': 'cls'}, 'ExpressionAttributeValues': {':0': {'S': 'tm58p168k6mbb658fdh530e7f2ci43njakqqb6o1'}, ':1': {'S': 'Item'}}, 'ReturnConsumedCapacity': 'TOTAL'}

Notice here we will get an error with the FilterExpression due to the fact that cls is not in the projection.

Whats the best way around this issue ?

ikonst commented 1 year ago

With this issue and #1000, I'm starting to think that adding the possible discriminator values as a filter expression wasn't the best idea.

hookercookerman commented 1 year ago

in the whole single table design hotness, there are many scenarios where a query will go across types, does this lead to a more abstracted Query object, I think it might.

it would also be nice to able to flag if I want deserialisation to happen or not or provide my own func to do so,

in terms of filter I think the filter condition is optional and defaulted not to happen,

if I know that for 100% that I have an entity in the items I am querying aka no indexes without projected values and I only want those items then I can flag it on to happern.

cheers