#389 ✓resolved
Piotr Solnica (solnic)

foreign key isn't being set even when the associated object has been assigned

Reported by Piotr Solnica (solnic) | June 15th, 2008 @ 06:39 AM

Here's a code which shows the problem:

class Shop
  include DataMapper::Resource

  property :id, Integer, :serial => true
  property :name, String, :length => 3..100

  auto_migrate!
end

class Product
  include DataMapper::Resource
  
  property :id, Integer, :serial => true
  property :shop_id, Integer, :index => true, :nullable => false
  property :name, String, :length => 3..100

  auto_migrate!
end

Shop.has(Shop.n, :products)
Product.belongs_to(:shop)

# Create a shop
shop = Shop.create(:name => 'My Shop')

# Try to create a product
product = Product.new(:name => 'Apple', :shop => shop)
product.save

puts product.valid? 	# => false
puts product.shop.name	# => My Shop
puts product.shop_id	# => nil

product.shop_id = shop.id
product.save

puts product.valid?	# => true
puts product.shop.name	# => My Shop
puts product.shop_id	# => 1

A desired behavior is that the foreign key should be set, if the associated object is assigned and already exists in the repository, otherwise DM should save the associated object, then set the FK in the child object.

Comments and changes to this ticket

  • Sam Smoot

    Sam Smoot June 15th, 2008 @ 10:36 PM

    This is a guess:

    I'm pretty sure this is the ":nullable => false". It causes the record to be invalid, so nothings saved since the association assignment happens after validation.

    So, here's what I think we need to do:

    Validations should be stubbed out in dm-core.

    
    def save(validation_context)
      assign_defaults
      assign_foreign_keys
      if execute_validations(validation_context)
        new_record? ? create : update
      else
        false
      end
    end
    
    private
    def execute_validations(validation_context)
      # This is stubbed out so dm-validations can overwrite it.
      # So we're making validations a special case, not using
      # hooks.
      true
    end
    
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 23rd, 2008 @ 07:21 PM

    • State changed from “new” to “invalid”

    Piotr, I believe this ticket was resolved in #395, and was also reported in #400, which is also resolved.

    I'm closing this ticket for now, but if this is still a problem for you please add a comment here and I'll reopen the ticket.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 23rd, 2008 @ 07:22 PM

    • State changed from “invalid” to “resolved”

    Since this was a valid bug at one time I've marked it as resolved instead of invalid.

  • Piotr Solnica (solnic)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages