Creating with nested relationships not saving the ones not connected directly to it #220

Open
HoneyryderChuck opened this Issue Aug 31, 2012 · 1 comment

Comments

Projects
None yet
2 participants

Hi

Just came across something which doesn't seem right. Lets say a new instance "a" belongs to "b", and "b" has one "c". If I save "a", "b" is stored as well, but "c" remains surprisingly not stored.



require 'rubygems'

begin
  require 'bundler'
rescue LoadError => e
  STDERR.puts e.message
  STDERR.puts "Run `gem install bundler` to install Bundler."
  exit e.status_code
end

begin
  Bundler.setup
rescue Bundler::BundlerError => e
  STDERR.puts e.message
  STDERR.puts "Run `bundle install` to install missing gems"
  exit e.status_code
end

require 'dm-core'
require 'dm-types'
require 'dm-validations'
require 'dm-migrations'
require 'pp'

class A

  include DataMapper::Resource

  property :id, Serial

  belongs_to :b

end

class B

  include DataMapper::Resource

  property :id, Serial

  has 0..n, :as

  has 1, :c

end

class C

  include DataMapper::Resource

  property :id, Serial

  belongs_to :c

end

DataMapper.setup(:default, 'sqlite:bug.db')
DataMapper.finalize.auto_migrate!

# ****************************** BUGGY CODE ******************************

a = A.new
a.b = B.new
a.b.c = C.new
a.save

p a #=> has id
p b #=> has id
p c #=> does not have id

# ****************************** BUGGY CODE ******************************


Even though I'm writing an example with sqlite I tested this in MySQL (just in case it influences something).

I ran into this as well. Interestingly enough, it does do multiple levels, but not in all directions. In other words, if you had b belongs to c instead of c belongs to b, your example would work. To put it another way, if you save an object it'll save its "parent" and "grandparent" but not its "sibling" (i.e. its parent's child.)

Either way, this seems wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment