#1526 new
Gavin

Switching Database error

Reported by Gavin | October 26th, 2015 @ 04:51 AM

When I have only a single database, I'm able to query all the records of a model that is a subclass of a module.

Below shows the irb output of a single database.

2.2.1 :050 > DataMapper.repository(:default){ MyMod::Person.all }
 => [#<MyMod::Person @id=1 @first_name="gavin" @last_name="yap" @age=35 @created=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)> @modified=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)>>] 


2.2.1 :051 > MyMod::Person.all => [#<MyMod::Person @id=1 @first_name="gavin" @last_name="yap" @age=35 @created=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)> @modified=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)>>]

But when I have multiple database, switching repositories, the errors makes no sense. While I can query a single record, I cannot query all.

>>  DataMapper.repository(:mytest){MyMod::Person.first }
=> #<MyMod::Person @id=1 @first_name="gavin" @last_name="yap" @age=35 @created=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)> @modified=#<DateTime: 2015-10-26T15:06:13+08:00 ((2457322j,25573s,0n),+28800s,2299161j)>>

>>  DataMapper.repository(:mytest){MyMod::Person }
=> MyMod::Person

>>  DataMapper.repository(:mytest){MyMod::Person.all }
ArgumentError: +options[:field]+ entry :id does not map to a property in MyMod::Person
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:805:in `block in assert_valid_fields'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:796:in `each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:796:in `assert_valid_fields'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-aggregates-1.2.0/lib/dm-aggregates/query.rb:23:in `assert_valid_fields_with_operator'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:768:in `block in assert_valid_options'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:766:in `each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:766:in `assert_valid_options'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/query.rb:727:in `initialize'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/adapters/abstract_adapter.rb:172:in `new'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/adapters/abstract_adapter.rb:172:in `new_query'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/repository.rb:131:in `new_query'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:666:in `query'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:656:in `collection_for_self'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:631:in `collection'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/collection.rb:507:in `block in each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `block in each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:411:in `each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/collection.rb:505:in `each'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/collection.rb:977:in `map'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/dm-core-1.2.1/lib/dm-core/collection.rb:977:in `inspect'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/gems/racksh-1.0.0/bin/racksh:21:in `<top (required)>'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/bin/racksh:23:in `load'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/bin/racksh:23:in `<main>'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /home/gavinyap/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'>>

My Person class is as follows, which will be loaded to my module "MyMod" and becomes MyMod::Person. There is a mymod_people table with the following attributes as well.

class Person
    include DataMapper::Resource

property :id,             Serial    
property :first_name,     String    
property :last_name,      String    
property :age,            Integer      # Code 
property :created,        DateTime  # Time created
property :modified,       DateTime  # Time modified



end

No comments found

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