Dynamoid icon indicating copy to clipboard operation
Dynamoid copied to clipboard

Problem with range-querying

Open carpodaster opened this issue 12 years ago • 6 comments

As the range key paragraph in the README suggests, I'm searching for past documents:

LogEntry.where("created_at.lt" => (DateTime.now - 1.week)).all

What I get in return is:

AWS::DynamoDB::Errors::ValidationException: One or more parameter values were invalid: An AttributeValue may not contain an empty string.
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/core/client.rb:273:in `return_or_raise'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/core/client.rb:372:in `client_request'
from (eval):3:in `query'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/dynamo_db/item_collection.rb:788:in `_each_item'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/core/collection/limitable.rb:57:in `each_batch'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/core/collection.rb:64:in `each'
from ~/.rvm/gems/ruby-1.9.2-p180/gems/aws-sdk-1.5.2/lib/aws/dynamo_db/item_collection.rb:496:in `each'
from ~/.rvm/gems/ruby-1.9.2-p180/bundler/gems/Dynamoid-61de8e9ce41e/lib/dynamoid/adapter/aws_sdk.rb:180:in `each

My Dynamoid document looks like this:

class LogEntry
  include Dynamoid::Document
  field :query
  index :created_at, range: true
end

The table was created implicitly by Dynamoid upon first access.

Is something wrong with the gem or am I missing something?

carpodaster avatar May 16 '12 12:05 carpodaster

I'm having this same issue:

class NameView
  include Dynamoid::Document

  table :name => :name_views

  field :name_id, :integer

  index :name_id
  index :created_at, :range => true
end

And here's the query:

NameView.where("created_at.gt" => (DateTime.now - 1.day)).all.count

hhunter avatar Sep 01 '12 14:09 hhunter

When fetching items from DynamoDB by range key, you must also specify the hash key.

loganb avatar Nov 28 '12 18:11 loganb

Hi Loganb - I have the same issue. Could you please give an example call?

leisun avatar Feb 03 '13 07:02 leisun

Just stepped through the lines, and I think this is a bug.

Is the following spec covering the actual case? => see @chain.query = {"created_at.gt" => @time - 1.day}

it 'finds matching index for a range query' do @chain.query = {"created_at.gt" => @time - 1.day} @chain.send(:index).should == User.indexes[[:created_at]]

@chain.query = {:name => 'Josh', "created_at.lt" => @time - 1.day}
@chain.send(:index).should == User.indexes[[:created_at, :name]]

end

See log output: => Notice :hash_key_value=>{:s=>""}

[AWS DynamoDB 400 0.399947 0 retries] query(:attributes_to_get=>["id","range"],:consistent_read=>false,:hash_key_value=>{:s=>""},:range_key_condition=>{:attribute_value_list=>[{:n=>"1359791559.0"}],:comparison_operator=>"GT"},:table_name=>"Yozio_DEV__index_user_created_ats") AWS::DynamoDB::Errors::ValidationException One or more parameter values were invalid: An AttributeValue may not contain an empty string.

See exception AWS::DynamoDB::Errors::ValidationException: One or more parameter values were invalid: An AttributeValue may not contain an empty string. from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/core/client.rb:318:in return_or_raise' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/core/client.rb:419:inclient_request' from (eval):3:in query' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/dynamo_db/item_collection.rb:788:in_each_item' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/core/collection/with_limit_and_next_token.rb:54:in _each_batch' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/core/collection.rb:82:ineach_batch' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/core/collection.rb:49:in each' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/aws-sdk-1.8.1.1/lib/aws/dynamo_db/item_collection.rb:496:ineach' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/adapter/aws_sdk.rb:223:in each' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/adapter/aws_sdk.rb:223:inquery' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/adapter.rb:243:in query' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/criteria/chain.rb:183:inids_from_index' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/criteria/chain.rb:163:in records_with_index' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/criteria/chain.rb:151:inrecords' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/dynamoid-0.6.1/lib/dynamoid/criteria/chain.rb:47:in all' from (irb):41 from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.11/lib/rails/commands/console.rb:47:instart' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in start' from /Users/leisun/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.11/lib/rails/commands.rb:41:in<top (required)>' from script/rails:6:in `require'

leisun avatar Feb 03 '13 08:02 leisun

I have the same issue. Anyone has a solution for it? EDIT: as loganb pointed out, you need to specify the has key, something like this

class StatsDynamo
  include Dynamoid::Document

  table :name => :stat_display_dev, :key => :id


  field :event_name

  index :event_name
  index :event_name, :range_key => :created_at
  index :created_at, :range => true

end

you'll be able to query in this way

StatsDynamo.where(event_name: "click").where("created_at.gt" => DateTime.now - 1.hour).all

bl4d3 avatar Aug 21 '13 08:08 bl4d3

This should be included in the example.. I spent way to much time trying to figure this out..

BernardGatt avatar Oct 11 '13 14:10 BernardGatt