所以我在DataMapper和對象關聯方面遇到了一些麻煩。 (代碼在底部提供)。我在保存的時候遇到了錯誤,一些id沒有設置,我認爲這與我如何設置我的關聯/不完全理解DataMapper中的關聯如何工作有關。導致保存失敗的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
而且我recieving的錯誤是:
~ (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
編輯好了,這是基本的代碼http://www.pastie.org/private/pwmihpa6vu3h7lypx64ag
其餘我永遠無法知道多少帖子,對不起!
是否存在堆棧跟蹤?你能縮小哪條線觸發它嗎? – Tim
您應該嘗試將問題簡化爲失敗的一件事,並在問題中發佈所有相關代碼。 –
請注意,'scrape_and_update'中的跟蹤源(我們沒有代碼)可以發佈該方法嗎? – Tim