This is my schema:
Posts:
id
roomsId
Rooms:
id
RoomEvents:
id
roomId
userId
created_at
The query I'm writing is:
action MyAction { .. } = do
.
.
roomEvent <- query @RoomEvent
|> filterWhere (#roomId, roomId)
|> orderBy #createdAt
|> fetchOneOrNothing
>>= fetchRelated #userId
But this is throwing following error:
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’
Does this mean that instances generated from other tables is interfering with this query?
I've tried using fetchRelatedOrNothing
and maybeFetchRelatedOrNothing
too.
UPDATE:
On using maybeFetchRelatedOrNothing
, I'm getting:
Couldn't match type ‘Maybe fieldValue0’ with ‘Id' "users"’
arising from a use of ‘maybeFetchRelatedOrNothing’
And on using fetchRelatedOrNothing
:
• Couldn't match type ‘Include "userId" (Maybe RoomEvent)’
with ‘RoomEvent' (Id' "rooms") User’
Expected type: Maybe RoomEvent
-> IO (ModuleRoomEvent' (Id' "rooms") User)
Actual type: Maybe ModuleRoomEvent
-> IO (Include "userId" (Maybe RoomEvent))
CodePudding user response:
Using maybeFetchRelatedOrNothing
should work here:
roomEvent <- query @RoomEvent
|> filterWhere (#roomId, roomId)
|> orderBy #createdAt
|> fetchOneOrNothing
>>= maybeFetchRelatedOrNothing #userId
What was the error when you tried this?
Also the |> orderBy #createdAt
requires a created_at
column in the room_events
table, but it's not listed as part of your schema. Maybe this could also affect the issue?