Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Belongs_to association saved too early #159

Open
lisad opened this issue Oct 7, 2013 · 1 comment
Open

Belongs_to association saved too early #159

lisad opened this issue Oct 7, 2013 · 1 comment

Comments

@lisad
Copy link
Contributor

lisad commented Oct 7, 2013

I'm using the console to debug what's going on, and from the stack trace it's clear that as soon as I create a belongs_to association, dynamoid tries to save it to the DB. Shouldn't that be done when the object is saved?

For example, code like this could be executed when creating a form to create a new plan belonging to an event, the Plan instance is created but not saved.

lisa:~/dev/clowdserv$ rails console
Loading development environment (Rails 4.0.0)
1.9.3p448 :001 >  e = Event.create(:event_id => "a3850a40-2f6e-11e3-aa6e-0800200c9a66")
 => #<Event:0x007fce4caa30f8 @new_record=false, @attributes={:created_at=>Mon, 07 Oct 2013 10:41:11 -0700, :updated_at=>Mon, 07 Oct 2013 10:41:11 -0700, :event_id=>"a3850a40-2f6e-11e3-aa6e-0800200c9a66", :plans_ids=>nil, :title=>nil, :status=>nil, :user_id=>nil}, @associations={}, @changed_attributes={}, @validation_context=nil, @errors=#<ActiveModel::Errors:0x007fce4ca27250 @base=#<Event:0x007fce4caa30f8 ...>, @messages={}>, @previously_changed={"event_id"=>[nil, "a3850a40-2f6e-11e3-aa6e-0800200c9a66"]}> 
1.9.3p448 :002 > p = Plan.new
 => #<Plan:0x007fce4955a6a8 @new_record=true, @attributes={:created_at=>nil, :updated_at=>nil, :plan_id=>nil, :event_ids=>nil, :title=>nil, :location_title=>nil, :location_address=>nil, :start=>nil, :end=>nil}, @associations={}> 
1.9.3p448 :003 > p.event = e
AWS::DynamoDB::Errors::ValidationException: One or more parameter values were invalid: Missing the key event_id in the item
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/aws-sdk-1.19.0/lib/aws/core/client.rb:366:in `return_or_raise'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/aws-sdk-1.19.0/lib/aws/core/client.rb:467:in `client_request'
    from (eval):3:in `put_item'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/aws-sdk-1.19.0/lib/aws/dynamo_db/item_collection.rb:186:in `create'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/adapter/aws_sdk.rb:196:in `put_item'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (3 levels) in <module:Adapter>'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/adapter.rb:39:in `benchmark'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (2 levels) in <module:Adapter>'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/adapter.rb:58:in `write'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/persistence.rb:284:in `block in persist'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:383:in `_run__2844610546378516575__save__callbacks'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/persistence.rb:266:in `persist'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/persistence.rb:163:in `block in save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:383:in `_run__2844610546378516575__create__callbacks'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/persistence.rb:163:in `save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/validations.rb:17:in `save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/identity_map.rb:72:in `save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/dirty.rb:13:in `block in save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/dirty.rb:28:in `clear_changes'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/dirty.rb:13:in `save'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/fields.rb:113:in `update_attribute'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/associations/single_association.rb:18:in `delete'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/associations/single_association.rb:11:in `setter'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/dynamoid-0.7.1/lib/dynamoid/associations.rb:98:in `block in association'
    from (irb):3
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/lisa/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'1.9.3p448 :004 > 
@loganb
Copy link
Collaborator

loganb commented Oct 7, 2013

Yup, that seems pretty buggy. I couldn't speak to how easy that is to fix.

At the moment, my energy is focused on migrating to the adapter code to the V2 DynamoDB client, but I'm happy to merge a PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants