1

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.

Ishan
  • 3,303
  • 5
  • 29
  • 47
  • same issue at my side getting answer from boto3, but pynamodb query on partition key is not working – Arpit Singh Oct 28 '21 at 14:27
  • Have you tried casting the value of `default_for_new` from UUID to a string? I wasn't able to repro this issue since using `uuid.uuid4()` as the default value gives an error on save: `TypeError: Object of type UUID is not JSON serializable`. Everything works as expected when the uuid is cast to a string. If the issue persists, please add any additional setup that might be useful to reproduce the issue, including how the db is populated. – Ben Dec 15 '21 at 06:23

0 Answers0