Yesod: Trying to print table data and getting foreign key information from another table

100 Views Asked by At

I am working with Yesod and I am trying to print a table containing a list of events and the city they belong to.

I have the following models:

City
    name Text
    countryId CountryId
    UniqueCity name
    deriving Eq
    deriving Show

Event
    title Text
    description Text
    date UTCTime
    cityId CityId
    deriving Eq
    deriving Show`

and the following code in the handler:

`module Handler.Event where

import Import
import Yesod.Form.Bootstrap3
import Database.Persist.Sql

getEventR :: Handler Html
getEventR = do
    events <- runDB $ selectList [] [Asc EventTitle]
    defaultLayout $ do 
        setTitle "Events"   
        [whamlet|
            <h1>Events
                <table>
                    <thead>
                        <tr>
                          <th>Event title</th>
                          <th>Event date</th>
                          <th>Event city</th>
                    <tbody>
                    $forall Entity eventId event <- events
                        <tr>
                            <td>#{eventTitle event}
                            <td>#{show (eventDate event)}   
                            <td>#{addCity eventId}   
        |]

addCity event = do
    city <- getCity $ eventCityId event
    return city

getCity cid = do
    city <- runDB $ get404 cid
    return city`

But when I try running this, I get the following error: Couldn't match expected type 'Event' with actual type 'Key Event' In the first argument of 'addCity', namely 'eventId_ajRi' In the first argument of 'toHtml', namely 'addCity eventId_ajRi'

I think I understand the error, but I really have no idea how to fix it and how to display the city name. Can someone please tell me how this should be done?

I have also tried doing it like this, but still no luck:

module Handler.Event where

import Import
import Yesod.Form.Bootstrap3
import Database.Persist.Sql

getEventR :: Handler Html
getEventR = do
    events <- runDB $ selectList [] [Asc EventTitle]
    defaultLayout $ do 
        setTitle "Events"   
        [whamlet|
            <h1>Events
                <table>
                    <thead>
                        <tr>
                          <th>Event name</th>
                          <th>Event date</th>
                          <th>Event city</th>
                    <ul>
                    <tbody>
                    $forall ev <- map entityVal events
                        <tr>
                            <td>#{eventTitle ev}
                            <td>#{show (eventDate ev)}   
                            <td>#{addCity ev}   
        |]

addCity ev = do
    eve <- entityVal ev
    city <- getCity $ eventCityId eve
    return city

getCity cid = do
    city <- runDB $ get404 cid
    return $ cityName city

With this modifications, I am now getting:

Couldn't match expected type 'Entity (HandlerT site0 IO Event)' with actual type 'Event'
In the first argument of 'addCity', namely 'ev_ajRc'
In the first argument of 'toHtml', namely 'addCity ev_ajRc'
0

There are 0 best solutions below