Haskell 在hamlet中如何通过外键获取其他表的值?
我有数据书和作者,书有外键authord。 我想把DB的书列出来。我需要作者的姓名,但book实体只有作者id,这就是为什么我使用Haskell 在hamlet中如何通过外键获取其他表的值?,haskell,persistence,yesod,Haskell,Persistence,Yesod,我有数据书和作者,书有外键authord。 我想把DB的书列出来。我需要作者的姓名,但book实体只有作者id,这就是为什么我使用get函数来获取作者数据,但在Hamlet文件中我无法获取作者的姓名,因为get函数返回的可能是作者 让我们看看我的代码: Book isbn Text title Text description Textarea Maybe author AuthorId UniqueBook isbn Author name Text
get
函数来获取作者数据,但在Hamlet文件中我无法获取作者的姓名,因为get
函数返回的可能是作者
让我们看看我的代码:
Book
isbn Text
title Text
description Textarea Maybe
author AuthorId
UniqueBook isbn
Author
name Text
UniqueAuthor name
请求的get函数:
getBookListR :: Handler Html
getBookListR = do
books <- runDB $ selectList [BookTitle !=. ""] []
defaultLayout $ do
$(widgetFile "booklistpage")
我原以为$可能会帮助我,但也许我误解了这个概念。我想了解为什么这段代码不起作用,以及当我们在哈姆雷特文件中迭代时,我们只有密钥时,这类情况的解决方案是什么。实际上我最近读了一本Yesod的书,这本书几乎完全涵盖了这类情况。谢谢Micheal!我有你的书《2012年的奥雷利一号》。谢谢。我们正在制作第二版,以推出最新的更改,因此请继续关注未来几个月左右的更新。
$if not $ null books
<table .table>
$forall Entity bookId book <- books
<tr>
<th th rowspan="4">Image
<td> #{bookTitle book}
<tr>
<td>
$maybe author <- get (bookAuthor book)
#{authorName author}
<tr>
<td> #{bookIsbn book}
<tr>
<td>
$maybe description <- bookDescription book
#{description}
<td>
$maybe author <- get (bookAuthor book)
#{authorName author}
Handler\BookList.hs:9:19:
Couldn't match expected type `Author'
with actual type `Maybe Author'
In the first argument of `authorName', namely `author_afBtA'
In the first argument of `toHtml', namely `authorName author_afBtA'
In the first argument of `asWidgetT . toWidget', namely
`toHtml (authorName author_afBtA)'