PynamoDB query() returns 0 values while scan() does

1.5k Views Asked by At

I have declared a pretty basic model and testing out query and other functionalities. I can run scan() and get all of the objects from dynamoDB, but when I run query I don't get back anything. I looked at examples, documentations and searched on internet. Couldn't figure out what I am doing wrong.

Here is my model:

class Book(BaseModel):
    id = UnicodeAttribute(hash_key=True, null=False, default_for_new=uuid.uuid4())
    name = UnicodeAttribute(null=False)
    author = UnicodeAttribute(null=True)
    pages = NumberAttribute(null=True)

    class Meta:
        table_name = BOOKS_TABLE_NAME

Here is the code that I have written as sample:

# List all books - Passes
print('Fetching all books')
books = Book.scan()
for b in books:
    print(b.id)
    print(b.name)
    # Fetch single book - Failed
    print('Fetching single book')
    book = list(Book.query(b.id))
    print(book)

If I use boto3 to get the item I am able to fetch it.

book = dynamodb_client.get_item(
    TableName=BOOKS_TABLE_NAME,
    Key={
        'id': {
            'S': id
        },
    }
)
print(book)

I am not sure why accessing via boto3 is working but not via pynamoDB. Any guidance would be deeply appreciated.

0

There are 0 best solutions below