#1371 ✓resolved
Greg Campbell

SystemStackError (stack level too deep) when enumerating association

Reported by Greg Campbell | July 20th, 2010 @ 03:58 PM

I've pasted a script below which reproduces the problem (Question: Is it usually preferable to do this, or to add the script as an attachment?).

There may be a simpler reproduction case than this, of course. DataMapper 1.0.0, tested in Rubies 1.8.7, 1.9.1, JRuby, and Rubinius.

#!/usr/bin/env ruby
#
# A one file test to show a SystemStackError in certain cases with relationships
require 'rubygems'
require 'dm-core'
require 'dm-migrations'


# setup the logger
DataMapper::Logger.new($stdout, :debug)

# connect to the DB
DataMapper.setup(:default, 'sqlite3::memory:')

class Dealership
  include DataMapper::Resource
  property :id, Serial
  has n, :owners
  has n, :cars
  
  def related
    Dealership.by_owner_names(owners.map(&:name)) - self
  end

  def self.by_owner_names(names)
    all("owners.name" => names)
  end
end

class Car
  include DataMapper::Resource

  property :id, Serial
  property :model_name, String
  belongs_to :dealership

  def self.hybrid
    all(:model_name => "Prius")
  end
end

class Owner
  include DataMapper::Resource

  property :id, Serial
  property :name, String
  belongs_to :dealership
end

DataMapper.finalize.auto_migrate!

2.times do
  d = Dealership.new
  d.cars.new(:model_name => "Prius")
  d.cars.new(:model_name => "Hummer")
  d.owners.new(:name => "Joe")
  d.save
end
d1 = Dealership.first
 
# prints "[#<Dealership @id=2>] as expected
p d1.related

# prints [#<Car @id=3 @model_name="Prius" @dealership_id=2>] as expected
p d1.related.first.cars.hybrid

# prints [[#<Car @id=3 @model_name="Prius" @dealership_id=2>]] as expected
p d1.related.to_a.map {|d| d.cars.hybrid }

# raises a SystemStackError - appears to have something to do with LazyArray
p d1.related.map {|d| d.cars.hybrid }

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

Pages