[英]Persistent upsert does not compile
我正在嘗試使用PostgreSQL在Persistent中做一些看起來很簡單的操作:給定一個Attendance
記錄列表,用相同的唯一鍵覆蓋任何現有行,或者如果不存在則插入。 類型如下:
share [mkPersist sqlSettings, mkDeleteCascade sqlSettings, mkMigrate "migrateAllEvents"] [persistLowerCase|
Event json sql=events
description Text
date UTCTime
UniqueEvent date
Attendance json
attending Bool
eventId EventId
user UserId
UniqueAttendance eventId user
|]
我沒有密鑰,所以不能使用repsert
,但是我認為需要upsert
。 upsert
的文檔對我來說似乎有些模糊:如果記錄存在並且我將更新保留為空白,則我知道它將保留實體。 但這並不等同於repsert
的行為,是嗎? 它也沒有說不存在唯一性約束時會發生什么。
我試過的是
runDb (mapM_ ups atts)
where
-- ups rec = upsert rec [AttendanceAttending =. True] also doesn't work
ups rec = upsert rec [AttendanceAttending =. (attendanceAttending rec)]
但這會導致我不太了解的錯誤:
• Illegal equational constraint BaseBackend backend ~ SqlBackend
(Use GADTs or TypeFamilies to permit this)
• When checking the inferred type
ups :: forall (m :: * -> *) backend.
(BaseBackend backend ~ SqlBackend, PersistUniqueWrite backend,
MonadIO m) =>
Attendance -> ReaderT backend m (Entity Attendance)
知道導致此錯誤的原因是什么? upsertBy
也一樣。 我已經在同一文件中導入了Esqueleto,但hiding ((=.))
,因此它應該是持久的。
令人尷尬的是,這實際上就像添加{-# LANGUAGE TypeFamilies #-}
一樣簡單。 我之所以沒有這樣做,是因為我誤讀了錯誤消息,而我的其他模塊都不需要該擴展名。 謝謝,費奧多!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.