IHP — Как использовать fetch, связанный с fetchOneOrNothing?

#haskell #ihp

Вопрос:

Это моя схема:

 Posts: id roomsId   Rooms: id  RoomEvents: id roomId userId created_at  

Запрос, который я пишу, таков:

 action MyAction { .. } = do . . roomEvent lt;- query @RoomEvent  |gt; filterWhere (#roomId, roomId)  |gt; orderBy #createdAt  |gt; fetchOneOrNothing  gt;gt;= 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) =gt;  PostsController -gt; 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 =gt;  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 ‘(gt;gt;=)’, namely ‘fetchRelated #userId’   

Означает ли это, что экземпляры, созданные из других таблиц, мешают этому запросу? Я тоже пробовал использовать fetchRelatedOrNothing и maybeFetchRelatedOrNothing тоже.

ОБНОВЛЕНИЕ: Я ввел неправильный тип в свое представление, что привело к тому, что система типов сделала вывод о неправильном типе. Изменив тип в поле зрения, сразу же исправили проблему.

Ответ №1:

Использование maybeFetchRelatedOrNothing должно работать здесь:

 roomEvent lt;- query @RoomEvent  |gt; filterWhere (#roomId, roomId)  |gt; orderBy #createdAt  |gt; fetchOneOrNothing  gt;gt;= maybeFetchRelatedOrNothing #userId  

В чем была ошибка, когда вы попробовали это сделать?

Также |gt; orderBy #createdAt требуется created_at столбец в room_events таблице, но он не указан как часть вашей схемы. Может быть, это тоже может повлиять на проблему?