2011-09-20 110 views
1

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

其餘我永遠無法知道多少帖子,對不起!

+0

是否存在堆棧跟蹤?你能縮小哪條線觸發它嗎? – Tim

+0

您應該嘗試將問題簡化爲失敗的一件事,並在問題中發佈所有相關代碼。 –

+0

請注意,'scrape_and_update'中的跟蹤源(我們沒有代碼)可以發佈該方法嗎? – Tim

回答

1

好吧,我對繼承進行了嘲弄,並且應該閱讀更多的文檔。發生了什麼是我錯過了使用子類時需要的property :type, Discriminator。所以即使我的一個子類不需要與其關聯的設備,另一個子類也是這樣,所以這就是觸發錯誤的原因。

我的工作模型看起來像這樣:http://www.pastie.org/2564668

0

在猜測,該值device_dict[:network_address]是沒有得到正確設置 - 這個錯誤表示你插入NULL設備地址,它來自於你的create呼叫nil值。當您去創建設備條目時,請嘗試檢查device_dict中的值。

+0

但是該類不應該需要device_network_address,因爲它屬於用戶而不是設備。那麼,與我的遺傳有關的東西是搞砸了嗎? – axiixc

+0

好吧,我要把它塞到繼承物上,因爲我把它拿出來,讓所有的東西都成爲一個獨特的階級,現在一切運作良好。所以現在我想我應該去查找如何繼承DM的權利。 – axiixc