DataMapper 关联/验证导致保存失败

发布于 2024-12-06 03:31:40 字数 2597 浏览 0 评论 0 原文

所以我在 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-12-13 03:31:40

好的,所以我了解了继承,并且应该阅读更多文档。发生的事情是我错过了使用子类时所需的 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

寄居人 2024-12-13 03:31:40

据猜测,值 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 a NULL device address, which comes from a nil value in your create call. Try checking the values in device_dict when you go to create device entries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文