Persistent で 1 対多の関係を定義しましたが、外部キーの 1 つを入力として使用できるフォームを作成する方法がわかりませんでした。私のユースケースを次のように単純化します。
Person
name String
Car
personId PersonId
name Text
type Text
車用のフォームを生成しようとすると、personId のフィールド タイプは何になるでしょうか? 私はこのようなことを試みましたが、エラーが発生しました:
entryForm :: Maybe Car -> Form Car
entryForm car = renderDivs $ Car
<$> areq personIdField "Person" Nothing
<*> areq textField "Car Name" ( carName <$> car)
<*> areq textField "Type" ( carType <$> car)
上記を実行すると、次のエラーが表示されます。 Not in scope: `personIdField'.
私は試してみましたがintField
、それは言う:
Couldn't match expected type `KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person'
with actual type `Text'
Expected type: Field
m0
(KeyBackend
persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend Person)
Actual type: Field m0 Text
In the first argument of `areq', namely `intField'
In the second argument of `(<$>)', namely
`areq intField "Person" Nothing
理想的には、個人名のドロップ ダウンを入力する (数が多すぎない場合) か、多すぎる場合は自由形式のテキスト フィールド (オートコンプリートなど) を使用したいと考えています。ユーザーからの入力として外部キーを取得する方法について何か提案はありますか?
更新:
次のように selectField を使用してみましたが、これが正しく行われているかどうかはわかりません。それでもエラーが発生します。最初に、personId を取得する where ステートメントを作成しました。
where
personId = do
person <- runDB $ selectFirst [] [Asc PersonName]
case person of
Just (Entity pId p) -> return pId
-- Nothing -> ???
そして、最初のareqをに変更しました
<$> areq (selectField [("First", personId)]) "Person Name" Nothing
ありがとう!