Dynamoid / dynamoid

Ruby ORM for Amazon's DynamoDB.
MIT License
579 stars 195 forks source link

Dealing with operator IN, strange behavior #395

Open tsipa88 opened 4 years ago

tsipa88 commented 4 years ago

Hi there. I have simple model like

class Post
  include Dynamoid::Document

  field :text
  field :post_type
  field :user_id

  range :posted_at, :datetime

  global_secondary_index hash_key: :user_id, range_key: :posted_at, projected_attributes: :all
end

I need to solve two simple problems: 1) Get all posts for one user like: Post.where(user_id: 1).scan_index_forward(false) 2) Get all posts for multiple users like: Post.where('user_id.in': [1,2,3,4,5]).scan_index_forward(false)

...In the near future I will add pagination and filtering via post_type.

The firstone works well. But, when I use the secondone (with operator in), i'm getting error Not indexed attributes: :user_id.in and the ordering didn't work at all. The ordering (scan_index_forward) by posted_at in both cases is very important. Where did i miss? Thnks. Regards.

andrykonchin commented 4 years ago

Hmm, good question.

But, when I use the secondone (with operator in), i'm getting error Not indexed attributes: :user_id.in

Looks like it's just a warning, not an error.

and the ordering didn't work at all

Yeah. Again, looks like it's completely correct behavior. In DynamoDB sorting works only in Query API call, but Query requires equality condition for partition/hash key, e.g. user_id: 'a'. Condition 'user_id.in': [1,2,3,4,5] is only supported by Scan API call which doesn't support sorting.

So you can use sorting only with conditions like Post.where(user_id: 1).

Please note - we can use Query with condition like user_id: 'a' only because there is a global index with user_id as a hash key.

tsipa88 commented 4 years ago

Hi @andrykonchin. So, no chance for me, am i right? Thanks for reply.