Have I misunderstood? Model Find returning data that doesn't belong to that model. #475
AlexStansfield
started this conversation in
General
Replies: 1 comment 1 reply
-
When a find request is given without the fields needed to complete the full SK, find will do a query with "begins_with" using the leading static portion of the SK that is defined. This is very efficient as DynamoDB can identify the items directly from the keys. We don't do a filter based on the type as that would entail reading items and then filtering. You can probably do this by providing your type as a parameter. What we typically do is have a different leading static portion for the SK. i.e. Order vs OrderItem. i.e. for the order, use "order#" and the order item would be "item#${orderId}#${id}" |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm not sure if this is the same issues as described here #421.
However I'm trying to understand how OneTable handles a specific use case as it's not working as I had expected.
I have a table that has an Order model and an OrderItem model.
The Primary hash is
pk
and the Primary sort issk
. With these parts of the schema:Order:
Order Item
The intention was this:
model.find
with just thecompanyId
table.queryItems
by giving thepk
and also thesk
with a "begins" and usinggroupByType
to get the Order model and the OrderItem models (as described here - https://doc.onetable.io/api/table/examples/)The 2nd one seems to work fine.
But when I try the first it's giving me all the Order Items too, the Order items have their fields that are not in an Order stripped out, but are clearly Order items as they are missing fields that an Order has and an Order item doesn't
I had expected as I called the
find
on the model that it would check the_type
field to make sure what it was returning was of the correct model and strip out anything else.Have I misunderstood how this works or am I doing something wrong?
Beta Was this translation helpful? Give feedback.
All reactions