【问题标题】:Persistent upsert does not compile持久更新插入不编译
【发布时间】:2018-01-20 18:45:35
【问题描述】:

我正在尝试用 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。

【问题讨论】:

  • 那么你有没有按照编译器的建议去做?

标签: haskell haskell-persistent


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多