
Loading fails for STI classes with empty discriminator values
Reported by David Perkowski | June 13th, 2008 @ 01:09 PM
When the discriminator field in the database is empty, loading an object will fail.
BaseClass.all
results in a stack trace something like:
ArgumentError: +resource+ should be a DataMapper::Resource, but was Object
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/identity_map.rb:19:in `set'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/resource.rb:853:in `load'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/collection.rb:10:in `load'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/adapters/data_objects_adapter.rb:48:in `read_many'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/adapters/data_objects_adapter.rb:168:in `with_connection'
from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.9.1/lib/dm-core/adapters/data_objects_adapter.rb:40:in `read_many'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.1/lib/extlib/lazy_array.rb:89:in `[]'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.1/lib/extlib/lazy_array.rb:89:in `lazy_load!'
from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.1/lib/extlib/lazy_array.rb:29:in `inspect'
from /opt/local/lib/ruby/1.8/irb.rb:298:in `output_value'
.. etc ..
Comments and changes to this ticket
-
-
Dan Kubb (dkubb) June 14th, 2008 @ 12:59 PM
- State changed from new to invalid
- Assigned user changed from Sam Smoot to Dan Kubb (dkubb)
This was resolved yesterday by the following commit:
http://github.com/sam/dm-core/co...
I'm closing this ticket now, but if you sync up to the latest in git, and it still has not resolved your issue please add a comment and I'll re-open.
-
Piotr Solnica (solnic) June 14th, 2008 @ 01:42 PM
Unfortunately I'm still having issues. Currently I have to set type columns in before :create block, otherwise for top-level objects in my STI hierarchy the type field is set to an empty string (so it passes the validations, since it's not nil). If I don't set discriminator fields manually, then I get the error...
-
Dan Kubb (dkubb) June 14th, 2008 @ 08:46 PM
- Milestone cleared.
- State changed from invalid to open
I'm going to assume it's one of the DO adapters, please let me know if that isn't the case. I also assume you're running edge DM. Which adapter is it you're seeing this problem with?
Can you paste in what the actual INSERT statement looks like? I want to see if it's DM that is setting the value to an empty string, or if perhaps it's the DB that is defaulting to empty string.
Also could you paste in the model you're using?
-
Piotr Solnica (solnic) June 15th, 2008 @ 04:08 AM
Sure, it's easy to reproduce this bug, here's the code:
require 'rubygems' Gem.path.unshift('./gems') require 'dm-core' require 'dm-validations' DataMapper.setup(:default, 'mysql://root@localhost/dm_core_test') class Animal include DataMapper::Resource property :id, Integer, :serial => true property :name, String, :length => 3..100 property :type, Discriminator auto_migrate! end class Zebra < Animal; end marty = Zebra.new(:name => 'Marty') marty.save # => mysql adapter executes: INSERT INTO `animals` (`name`) VALUES (?) marty.type # => Zebra # causes: dm-core-0.9.2/lib/dm-core/identity_map.rb:18:in `set': +resource+ should be a DataMapper::Resource, but was Object (ArgumentError) Animal.first
using latest DO/DM stuff
-
Dan Kubb (dkubb) June 15th, 2008 @ 06:26 PM
- State changed from open to resolved
This ticket was resolved with the following commit:
http://github.com/sam/dm-core/co...
I am marking this ticket as resolved. If this did not resolve your issue, please comment here and I will re-open the 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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »