持久更新插入不编译

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是我需要的。 documentation for 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 ((=.)),所以它应该是纯 Persistent。

尴尬的是,真的是加{-# LANGUAGE TypeFamilies #-}那么简单。我没有尝试过,因为我误读了错误消息并且我的其他模块的 none 需要该扩展。谢谢,费奥多尔!