所以我在 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/ospnkeeyylul9mhf4fgxhdq
编辑 好的,这基本上是代码的其余部分http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
我永远不知道要发布多少内容,抱歉!
So I'm having some trouble with DataMapper and object associations. (Code provided at bottom). I'm getting errors upon saving to the extent that some id's are not set, which I think has something to do with how I setup my associations/not fully understanding how associations work under DataMapper.
The code I'm running is:
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
And the errors I am recieving are:
~ (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=)
Model classes are here: http://www.pastie.org/private/xer5grfaulmnxalne6g5va (link for brevity)
EDIT Okay the crash is coming from create on line 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]
)
So would it have something to do with the inheritance from BandwidthEntry/BandwidthDeviceEntry
, because that class doesn't even have an association with a device.
Might as well post the full stack trace too: http://www.pastie.org/private/ospnkeeylul9mhf4fgxhdq
EDIT Okay, here is basically the rest of the code http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
I can never tell how much to post, sorry!
发布评论
评论(2)
好的,所以我了解了继承,并且应该阅读更多文档。发生的事情是我错过了使用子类时所需的
property :type, Discriminator
。因此,即使我的一个子类不需要与之关联的设备,但另一个子类却需要,所以这就是触发错误的原因。我的工作模型如下所示: http://www.pastie.org/2564668
Okay, so I derped on inheritance, and should have read more of the documentation. What was happening was I missed the
property :type, Discriminator
needed when using subclasses. So even though my one subclass didn't need a device associated with it, the other did, so that's what was triggering the error.My working model looks like this: http://www.pastie.org/2564668
据猜测,值
device_dict[:network_address]
未正确设置 - 该错误表明您正在插入NULL
设备地址,该地址来自create
调用中的 >nil 值。当您创建设备条目时,请尝试检查device_dict
中的值。At a guess, the value
device_dict[:network_address]
isn't getting set properly - that error indicates you're inserting aNULL
device address, which comes from anil
value in yourcreate
call. Try checking the values indevice_dict
when you go to create device entries.