【发布时间】:2011-09-20 17:47:29
【问题描述】:
所以我在使用 DataMapper 和对象关联时遇到了一些问题。 (底部提供的代码)。我在保存到未设置某些 id 的情况下遇到错误,我认为这与我如何设置关联/不完全理解关联在 DataMapper 下的工作方式有关。
我运行的代码是:
Rose.setup_datamapper
Rose::User.first_or_create(:username => 'some-user', :password => 'REDACTED')
Rose::User.all.each do |user|
user.scrape_and_update
user.errors.each { |e| puts e } unless user.save
user.bandwidth_entries.each { |e| puts e }
end
我收到的错误是:
~ (0.000064) SELECT "id", "username", "password" FROM "rose_users" WHERE ("username" = 'some-user' AND "password" = 'REDACTED') ORDER BY "id" LIMIT 1
~ (0.000042) SELECT "id", "username", "password" FROM "rose_users" ORDER BY "id"
~ rose_bandwidth_entries.device_network_address may not be NULL (code: 19, sql state: , query: INSERT INTO "rose_bandwidth_entries" ("policy_mbytes_received", "policy_mbytes_sent", "actual_mbytes_received", "actual_mbytes_sent", "timestamp", "bandwidth_class", "user_id") VALUES (583.34, 39.58, 590.27, 44.26, '2011-09-20T13:39:31-04:00', 0.0, 1), uri: sqlite3:/Users/axiixc/Dropbox/Ruby/stats.sqlite?port=&adapter=sqlite3&fragment=&path=/Users/axiixc/Dropbox/Ruby/stats.sqlite&scheme=sqlite3&host=&user=&password=&query=)
模型类在这里:http://www.pastie.org/private/xer5grfaulmnxalne6g5va(为简洁起见链接)
编辑好的,崩溃来自第 26 行的创建:
# /Library/Ruby/Gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:114:in `execute_non_query': rose_bandwidth_entries.device_network_address may not be NULL (DataObjects::IntegrityError)
main_entry = BandwidthMainEntry.create(
:user => self,
:timestamp => Time.new,
:policy_mbytes_received => scrape_dict[:main][:policy_mbytes_received],
:policy_mbytes_sent => scrape_dict[:main][:policy_mbytes_sent],
:actual_mbytes_received => scrape_dict[:main][:actual_mbytes_received],
:actual_mbytes_sent => scrape_dict[:main][:actual_mbytes_sent],
:bandwidth_class => scrape_dict[:main][:bandwidth_class]
)
那么它是否与BandwidthEntry/BandwidthDeviceEntry 的继承有关,因为该类甚至与设备没有关联。
不妨也发布完整的堆栈跟踪:http://www.pastie.org/private/ospnkeeylul9mhf4fgxhdq
EDIT好了,剩下的代码基本就到这里了http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
我永远不知道要发布多少,抱歉!
【问题讨论】:
-
当它出现时是否有堆栈跟踪?你能缩小触发它的那条线吗?
-
您可能应该尝试将您的问题减少到失败的一件事,并在问题中发布所有相关代码。
-
请注意
scrape_and_update中的跟踪源,我们没有代码 - 您也可以发布该方法吗?
标签: ruby ruby-datamapper