2013-03-11 21 views
2

我正在使用persistent和persistent-mysql。我有一個單子SqlM持久性:將文本轉換爲密鑰

type SqlM a = SqlPersist (ResourceT IO) a) 

在我的功能

testFun :: T.Text -> SqlM() 
testFun someId = ... 

我可以查詢數據庫使用

entity <- selectFirst [SomeField ==. someId] 

但我想通過ID選擇實體來說。我必須將someId轉換/打包到Key - Type。我知道這是不是做到這一點,但我想:

entity <- get $ Key { unKey = PersistInt64 (read $ T.unpack someId) } 

這個失敗:

Couldn't match type `PersistEntityBackend 
         (Entity (DBTableGeneric backend0))' 
       with `Database.Persist.GenericSql.Raw.SqlBackend' 
The type variable `backend0' is ambiguous 
Possible fix: add a type signature that fixes these type variable(s) 
Expected type: Key (Entity (DBTableGeneric backend0)) 
    Actual type: KeyBackend 
       Database.Persist.GenericSql.Raw.SqlBackend 
       (Entity (DBTableGeneric backend0)) 
In the second argument of `($)', namely 
    `Key {unKey = PersistInt64 (read $ T.unpack someId)}' 

任何想法是怎麼回事?

回答

4

我通常使用fromPathPiece進行這種轉換。至於你的錯誤信息,你可能只需要添加一個類型簽名來向編譯器澄清你正在使用的類型。

+0

我有什麼要添加類型簽名?我試圖將它添加到鍵_KeyBackend和SqlBackend entity_,但我仍然得到上面顯示的錯誤。 – agrafix 2013-03-11 14:05:40

+0

嘗試類似'(fromPathPiece someId :: Maybe DBTableId)'。 – 2013-03-11 14:11:31

+0

非常感謝您的快速幫助,完成了這項工作! – agrafix 2013-03-11 14:17:46