IHP - 如何将 fetchRelated 与 fetchOneOrNothing 一起使用?

IHP - How to use fetchRelated with fetchOneOrNothing?

这是我的架构:

Posts:
id
roomsId


Rooms:
id

RoomEvents:
id
roomId
userId
created_at

我正在写的查询是:

action MyAction { .. } = do
.
.
roomEvent <- query @RoomEvent
  |> filterWhere (#roomId, roomId)
  |> orderBy #createdAt
  |> fetchOneOrNothing
  >>= fetchRelated #userId

但这是抛出以下错误:

Web/Controller/Posts.hs:150:21: error:
    • Could not deduce (FromRow fetchModel0)
        arising from a use of ‘fetchRelated’
      from the context: (?context::ControllerContext,
                         ?modelContext::ModelContext, ?theAction::PostsController)
        bound by the type signature for:
                   action :: (?context::ControllerContext,
                              ?modelContext::ModelContext, ?theAction::PostsController) =>
                             PostsController -> IO ()
        at Web/Controller/Posts.hs:57:5-10
      The type variable ‘fetchModel0’ is ambiguous
The type variable ‘fetchModel0’ is ambiguous
      These potential instances exist:
        instance Database.PostgreSQL.Simple.FromField.FromField a =>
                 FromRow (Only a)
          -- Defined in ‘Database.PostgreSQL.Simple.FromRow’
        instance FromRow Activity
          -- Defined at build/Generated/Types.hs:412:10
        instance FromRow ActivityPostFile
          -- Defined at build/Generated/Types.hs:802:10
        ...plus 64 others
        ...plus one instance involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the second argument of ‘(>>=)’, namely ‘fetchRelated #userId’

这是否意味着从其他表生成的实例正在干扰此查询?我也尝试过使用 fetchRelatedOrNothingmaybeFetchRelatedOrNothing

更新:我在我的视图中输入了错误的类型,导致类型系统推断出错误的类型。更改视图中的类型,立即解决了问题。

在这里使用 maybeFetchRelatedOrNothing 应该有效:

roomEvent <- query @RoomEvent
  |> filterWhere (#roomId, roomId)
  |> orderBy #createdAt
  |> fetchOneOrNothing
  >>= maybeFetchRelatedOrNothing #userId

您尝试此操作时出现什么错误?

此外,|> orderBy #createdAt 需要 room_events table 中的 created_at 列,但它未列为架构的一部分。也许这也会影响这个问题?