Dynamoid icon indicating copy to clipboard operation
Dynamoid copied to clipboard

It blows up when you define a custom hash key

Open esdras opened this issue 13 years ago • 6 comments

Hey guys, how is it going?

It is the first time I'm using Dynamoid and it looks like a found I bug. I'm not sure if I'm doing something wrong, but here is it goes:

The following model works fine:

class Dytest

  include Dynamoid::Document

  table name: "#{Rails.env}_preferences"

  field :foo
  field :bar

end

but if I set a custom hash key it breaks the query:

class Dytest

  include Dynamoid::Document

  table name: "#{Rails.env}_preferences", key: :entity_uuid

  field :foo
  field :bar

end

d = Dytest.new(foo: '42', bar: 'monkey')
d.save

Dytest.all #here it blows up

Backtrace:

NoMethodError: undefined method `split' for nil:NilClass
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:155:in `block (2 levels) in result_for_partition'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:153:in `each'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:153:in `block in result_for_partition'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:152:in `tap'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:152:in `result_for_partition'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/adapter.rb:115:in `scan'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/criteria/chain.rb:148:in `records_without_index'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/criteria/chain.rb:90:in `records'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/criteria/chain.rb:46:in `all'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/dynamoid-0.5.0/lib/dynamoid/criteria.rb:20:in `block (2 levels) in <module:ClassMethods>'
    from (irb):4
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/railties-3.1.4/lib/rails/commands/console.rb:45:in `start'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/railties-3.1.4/lib/rails/commands/console.rb:8:in `start'
    from /Users/esdrasmayrink/.rvm/gems/ruby-1.9.2-p320@organizze/gems/railties-3.1.4/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

It seems that this method is where it blows up.

# It seems that result[:id] is nil because I defined a custom hash key, am I right?
def result_for_partition(results)
  {}.tap do |hash|
    Array(results).each do |result|
      next if result.nil?
      id = result[:id].split('.').first
      if !hash[id] || (result[:updated_at] > hash[id][:updated_at])
        result[:id] = id
        hash[id] = result
      end
    end
  end.values
end

Please let me know if there is anything I can do to help.

Cheers, Esdras.

esdras avatar Nov 29 '12 14:11 esdras

I believe if you have partitioning on you are stuck with using id as the key.

A feature request could involve using the hash_key as the partitioned key instead of id.

cheneveld avatar Nov 30 '12 23:11 cheneveld

Thanks @cheneveld, I guess I'll set partitioning to false then.

esdras avatar Dec 03 '12 20:12 esdras

Thanks @cheneveld, I guess I'll set partitioning to false.

esdras avatar Dec 03 '12 20:12 esdras

Thanks @cheneveld https://github.com/cheneveld, I'll remove partitioning for now.

On Fri, Nov 30, 2012 at 9:37 PM, Craig Heneveld [email protected]:

I believe if you have partitioning on you are stuck with using id as the key.

A feature request could involve using the hash_key as the partitioned key instead of id.

— Reply to this email directly or view it on GitHubhttps://github.com/Veraticus/Dynamoid/issues/83#issuecomment-10908604.

esdras avatar Dec 03 '12 20:12 esdras

I wonder whether this named hash issue goes beyond partitioning. I turned off partitioning as described above, but couldn't add a range_key until I changed the hash_key back to :id.

This didn't work:

class MyData
  include Dynamoid::Document

  table :name => :my_data, :key => :user_id_group_id

  field :user_id_group_id, :string
  field :date, :integer

  index :date, :range => true
end

But this worked

class MyData
  include Dynamoid::Document

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

  field :date, :integer

  index :date, :range => true
end

guyshechter avatar Apr 17 '13 13:04 guyshechter

it works for me when i set the partitioning to false, as i defined a custom hash_key.

alex-zige avatar Aug 04 '14 23:08 alex-zige