İçeriğe geç
Community

KONULARI KEŞFET

Editoryal yaklaşım

CoreTrailKit ile yeni mi tanışıyorsun?

Servislerini izle. Kanıtları elinin altında tut.

CoreTrailKit
Yazılara dön
REHBER/ AWS

DynamoDB GetItem: fix an incomplete primary key

GetItem reports that the provided key does not match the schema, even though the partition-key value looks correct. Check whether the table also requires a sort key and whether each attribute uses the expected type.

Yazar: CoreTrailKit3 dk okuma

01

Inspect the actual table schema and target

Confirm the table name or ARN, account context, and Region before comparing keys. A similarly named table in another environment can have a different primary key. Inspect KeySchema and the types declared for those key attributes.

Our local table used pk as its partition key and sk as its sort key, both strings. Sending only pk failed with ValidationException before any missing-item result could be interpreted. The request did not identify the complete key.

sh · ÖRNEK
aws dynamodb describe-table --table-name YOUR_TABLE \
  --profile work --region eu-west-1 \
  --query '{KeySchema:Table.KeySchema,Types:Table.AttributeDefinitions}'

02

Match names and types at the API layer you use

Boto3’s Table resource accepts ordinary Python values for keys; the low-level client uses DynamoDB AttributeValue objects. Do not mix those two input formats. A numeric sort key and a string containing digits are different types.

Check capitalization and the exact key names. Adding an unrelated business identifier to the key map does not satisfy a missing primary-key attribute and can itself make the request invalid.

03

Supply the complete resource-style key

For the example string/string table, the following uses the high-level Table resource and a full key. The profile and Region are placeholders for an authorized application context; our reproduction used a local endpoint and synthetic credentials instead.

The lab’s corrected call returned the seeded amount after both key components were supplied. That result isolates request completeness for the declared schema, without changing data or replacing the table.

python · ÖRNEK
import boto3

db = boto3.Session(profile_name="work").resource("dynamodb", region_name="eu-west-1")
table = db.Table("YOUR_TABLE")
response = table.get_item(
    Key={"pk": "order#1", "sk": "summary"},
    ConsistentRead=True,
)
item = response.get("Item")

04

Distinguish no item from an invalid request

A valid GetItem for an absent item returns no Item member. A ValidationException is a different outcome and should not be handled as “not found.” Capture the service error code while avoiding logs that expose entire sensitive items.

If the request is valid but the result is unexpected, verify the full key, table target, and consistency requirements. A strongly consistent table read can be useful for an immediate verification, but it cannot repair a wrong key or account.

05

Use an access pattern that fits the question

If you know only the partition key and need its related items, consider Query with the table’s key conditions. GetItem does not search a partition for whichever sort key happens to exist. A secondary index also has its own access pattern; it is not a replacement table key for GetItem.

Keep the tested key schema and the request format together in a small regression case. This makes a future table or repository refactor fail clearly when code stops supplying the complete identifier, instead of disguising a schema error as missing data.

Sınırları bil

Reproduced with official DynamoDB Local 3.3.2 and Boto3 1.42.97. A table with string pk and sk rejected a GetItem containing only pk; the full key retrieved the seeded item. Local testing does not validate managed-service IAM, capacity, or regional routing.

Kaynaklar ve ileri okuma

CoreTrailKit tarafından hazırlanan teknik yayın.

Bütün yazıları keşfet

KEŞFETMEYE DEVAM ET