#943 ✓resolved
Sindre Aarsaether

STI error when adding relationships for the base_model

Reported by Sindre Aarsaether | July 2nd, 2009 @ 04:59 AM | in 0.10.0

See example below:


# encoding: utf-8

require 'rubygems'
require 'dm-core'
require "dm-is-tree"

DataMapper.setup(:default,
  :adapter => 'mysql',
  :host => 'localhost', 
  :username => 'root',
  :database => 'dm_core_test2',
  :encoding => 'UTF-8'
)

class Entity
  include DataMapper::Resource
  
  property :id, Serial
  property :type, Discriminator # Company, Organization, Person
  property :name, String

  has n, :children, :model => Entity
  belongs_to :parent, :model => Entity, :child_key => [:parent_id], :nullable => true
end

class Company < Entity
  
end

class Person < Entity
  
end

Entity.auto_migrate!

apple = Company.create(:name => "Apple Inc")
sjobs = Person.create(:name => "Steve Jobs", :parent => apple)
####
# /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:146:in 
#  `parent=': +target+ should be Person or NilClass, but was Company (ArgumentError)
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:266:in `send'
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:266:in `attributes='
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:262:in `each'
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:262:in `attributes='
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:734:in `initialize'
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:315:in `send'
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:315:in `new'
#   from /usr/local/lib/ruby/gems/1.8/gems/dm-validations-0.10.0/lib/dm-validations.rb:33:in `create'
#   from sti_scope_bug.rb:43

# PS, this also affects dm-is_tree. And dm-is_tree should also add 
# :nullable => true to the belongs-to relationship. Will file a separate bug for that

Comments and changes to this ticket

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 »

People watching this ticket

Referenced by

Pages