DataMapper表示TypeError:没有从nil到整数的隐式转换

时间:2014-06-07 16:45:41

标签: ruby sinatra datamapper

我有一个非常简单的表,几乎是一个键/值存储,类似于

class Device
    include DataMapper::Resource
    property :token,    Text, :key => true
end

当然我在定义之后调用了DataMapper.finalize,因为我使用的是Sinatra。

当我创建一个新设备时,就像这样

Device.create(:token => "Foo Bar")

我得到了所有这些巨大的转储

~ (0.000063) SET sql_auto_is_null = 0
~ no implicit conversion from nil to integer
TypeError: no implicit conversion from nil to integer
    from /Library/Ruby/Gems/2.0.0/gems/data_objects-0.10.14/lib/data_objects/quoting.rb:12:in `quote_string'
    from /Library/Ruby/Gems/2.0.0/gems/data_objects-0.10.14/lib/data_objects/quoting.rb:12:in `quote_value'
    from /Library/Ruby/Gems/2.0.0/gems/data_objects-0.10.14/lib/data_objects/command.rb:69:in `block in escape_sql'
    from /Library/Ruby/Gems/2.0.0/gems/data_objects-0.10.14/lib/data_objects/command.rb:62:in `gsub!'
    from /Library/Ruby/Gems/2.0.0/gems/data_objects-0.10.14/lib/data_objects/command.rb:62:in `escape_sql'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:114:in `execute_non_query'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:114:in `block (2 levels) in create'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:113:in `block in create'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:85:in `each'
    from /Library/Ruby/Gems/2.0.0/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:85:in `create'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/repository.rb:146:in `create'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource/persistence_state/transient.rb:74:in `create_resource'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource/persistence_state/transient.rb:25:in `commit'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:963:in `_persist'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:977:in `block in create_with_hooks'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:974:in `catch'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:974:in `create_with_hooks'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:1028:in `save_self'
    from /Library/Ruby/Gems/2.0.0/gems/dm-validations-1.2.0/lib/dm-validations.rb:54:in `save_self'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:1013:in `block in _save'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:1229:in `run_once'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:1012:in `_save'
    from /Library/Ruby/Gems/2.0.0/gems/dm-core-1.2.1/lib/dm-core/resource.rb:412:in `save'
    from /Library/Ruby/Gems/2.0.0/gems/dm-validations-1.2.0/lib/dm-validations.rb:40:in `block in save'
    from /Library/Ruby/Gems/2.0.0/gems/dm-validations-1.2.0/lib/dm-validations/context.rb:16:in `in_context'
    from /Library/Ruby/Gems/2.0.0/gems/dm-validations-1.2.0/lib/dm-validations.rb:40:in `save'
    from /Library/Ruby/Gems/2.0.0/gems/dm-validations-1.2.0/lib/dm-validations.rb:130:in `create'
    from (irb):19
    from /usr/bin/irb:12

当然,当我在Sinatra中运行完全相同的代码时,会发生类似的事情。我正在运行Ruby 2.0,以防它不清楚。

任何线索?

编辑:由于时间限制,我已经搬到了Rails,但我喜欢Sinatra,我仍然喜欢这项工作,所以如果有人有想法,我仍然非常感谢欢迎他们。

2 个答案:

答案 0 :(得分:1)

我花了好几个小时把头发拉出来试图在Ubuntu 14.04上克服这个确切的问题(可靠)。这似乎完全没有意义,试图调试数据对象中的问题宝石是徒劳的。

我的解决方案:卸载libmariadbclient-dev,安装libmysqlclient-dev,重新部署,最终有效。

希望这有帮助。

答案 1 :(得分:1)

我遇到了完全相同的问题,并且还使用了Ruby 2.0(OS X Mavericks上的默认ruby)。升级到2.1.2修复了问题。

如果您实际使用OS X,我建议您通过RVM安装

\curl -sSL https://get.rvm.io | bash -s stable --ruby