#1045 ✓resolved
Sindre Aarsaether

Error with SEL and STI (again)

Reported by Sindre Aarsaether | September 17th, 2009 @ 01:54 PM | in 0.10.1

I believe this was recently introduced..

require 'rubygems'
require 'dm-core'
  :adapter => 'mysql',
  :host => 'localhost', 
  :username => 'root',
  :database => 'dm_core_test',
  :encoding => 'utf8'
DataObjects::Mysql.logger = DataObjects::Logger.new(STDOUT, :debug)

class Entity
  include DataMapper::Resource
  property :id, Serial
  property :type, Discriminator
  property :name, String
  belongs_to :country, :nullable => true

class Company < Entity; end
class Person < Entity; end

class Country
  include DataMapper::Resource
  property :id, Serial
  property :name, String
  has n, :entities

us = Country.create(:name => "USA")
hu = Country.create(:name => "Hungary")

c = Company.create(:name => 'Whole Foods', :country => us)
p = Person.create(:name => 'John')
p = Person.create(:name => 'Fredy', :country => hu)

Entity.all.each{|e| e.country}
# Thu, 17 Sep 2009 18:50:57 GMT ~ debug ~ (0.000160) SELECT `id`, `name` FROM `countries` WHERE `id` IN (1, NULL, 2) ORDER BY `id`
# /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:217:in `eager_load_targets': +source+ should be Company, but was Person (ArgumentError)
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:293:in `block in eager_load'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:291:in `each'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/relationship.rb:291:in `eager_load'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:196:in `lazy_load'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:96:in `get'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_one.rb:160:in `country'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `each'
#   from /usr/local/ruby-1.9.1-p129/lib/ruby/gems/1.9.1/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `each'
#   from relationship_2.rb:50:in `<main>'

See it in a gist here: http://gist.github.com/188654

Is STI specced at all in datamapper? Almost the relationship-bugs I have discovered the last few months have been related to STI and/or SEL.

Comments and changes to this ticket

  • Sindre Aarsaether

    Sindre Aarsaether September 23rd, 2009 @ 09:17 AM

    The fixes I mentioned in irc didn't really seem to do anything, other than not raising the assertion-error.
    What happens is that DM is trying to eager load a whole collection of a top-level model, with the relationship of the first resource that kicks the load.

    Entity.all returns both companies and people
    When doing Entity.all.each{|e| e.country } it kicks the country-relationship of Company, not Entity.
    Entity.relationships[:country] != Company.relationships[:country]

    Don't really know how to solve it, but it did work at some point a few weeks ago.

  • Andrea Dallera

    Andrea Dallera September 23rd, 2009 @ 09:30 AM

    Hei Sindre,

      i've got the same exact issue. Everything was working fine before 0.10. My sample code attached.
  • Martin Gamsjaeger (snusnu)

    Martin Gamsjaeger (snusnu) September 23rd, 2009 @ 04:39 PM

    • Tag changed from 0.10.0, discriminator, many-to-one, sel to 0.10.0, discriminator, many-to-one, regression, sel
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) September 27th, 2009 @ 12:49 AM

    • State changed from “unconfirmed” to “confirmed”
    • Milestone set to 0.10.1
    • Assigned user set to “Dan Kubb (dkubb)”

    I've attached a patch that I believe fixes this issue, but I'd like a few people to apply it to dm-core locally, and see if it resolves the problem on their end.

    Assuming this does fix the problem, I will add more specs that duplicate the problem Sindre reported, so that this regression cannot happen again in the future.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) September 28th, 2009 @ 11:17 AM

    • State changed from “confirmed” to “resolved”

    (from [d73f3a0e7bd99b94d04c2ff8b0236e342896c7c9]) Simplified Relationship inheritance by subclasses

    • Fixed bug where SEL and STI would not work together if the sources were from sibling classes
    • Added failing specs for SEL w/STI subclasses
    • Updated Property inheritance to use similar logic for consistency

    [#1045 state:resolved] http://github.com/datamapper/dm-core/commit/d73f3a0e7bd99b94d04c2ff...

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) September 28th, 2009 @ 12:17 PM

    I pushed the above commit because I was able to reproduce the problem within a spec, but I'd still like for Sindre or someone else with this problem to confirm if this problem is resolved for them.

  • Andrea Dallera

    Andrea Dallera September 29th, 2009 @ 05:42 AM

    I fetched from github and installed the latest version and it fixes the bug. Thanks a lot.

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 »


Referenced by