There are some answers in some forums about this question where people are generally repeating the already confusing AWS documentation about DynamoDB, but I need an example to see how it actually works. I have this query :
SELECT id, message, created, FROM "messages"."sender_company_id-index"
WHERE sender_company_id = 435634652 AND receiver_company_id = 69992528
AND sender_user_id = 186 AND receiver_user_id = 201
ORDER BY id DESC
and it fails with this error message:
An error occurred during the execution of the command. ValidationException: Variable reference id in ORDER BY clause must be part of the primary key
created field is already indexed so what's wrong? Why does it require it to be "part of the primary key"? I would like to order by different fields in different cases so I cant have all of them set as primary key. I dont get it!
EDIT
I noticed some of the comments below state that ORDER BY is not a possibility using PartiQL. Also this SO link claims that. However, AWS official documentation states the opposite.
UPDATE:
I was able to make it work though not completely.
SELECT id, created, sender_company_id, receiver_company_id
sender_user_name, sender_user_id, sender_company_name, receiver_company_name
FROM "messages"."sender_company_id-created-index"
WHERE (sender_company_id = 435634652 OR receiver_company_id= 435634652)
AND (sender_user_id = 186 OR receiver_user_id = 186)
ORDER BY created ASC, sender_company_id ASC
Error:
ValidationException: Must have at least one non-optional hash key condition in WHERE clause when using ORDER BY clause.
If I change the WHERE to this it works:
...WHERE sender_company_id = 435634652
Here is my indexes:
It is beyond my understanding what's going on here. I do have the indexes, I am using non-optional (key partitions) in the WHERE clause but it fails.