Skip to content

Commit

Permalink
fix a problem preventing use of Primary with NoFieldselectors (#1460)
Browse files Browse the repository at this point in the history
* fix a problem preventing use of Primary with NoFieldselectors

* bump the version to 2.14.4.4 and update ChangeLog.md
  • Loading branch information
fumieval authored Jan 5, 2023
1 parent 7222127 commit bea2018
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for persistent

## 2.14.4.4

* [#1460] https://github.com/yesodweb/persistent/pull/1460
* Fix a problem where a `Primary` key causes `mkPersist` to generate code
that doesn't compile under `NoFieldSelectors`

## 2.14.4.3

* [#1452](https://github.com/yesodweb/persistent/pull/1452)
Expand Down
15 changes: 6 additions & 9 deletions persistent/Database/Persist/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,8 +1847,10 @@ mkKeyToValues mps entDef = do
ListE <$> mapM (f recName) (toList $ unboundCompositeCols ucd)
f recName fieldNameHS =
[|
toPersistValue ($(varE $ keyFieldName mps entDef fieldNameHS) $(varE recName))
toPersistValue ($(pure $ keyFieldSel fieldNameHS) $(varE recName))
|]
keyFieldSel name
= fieldSel (keyConName entDef) (keyFieldName mps entDef name)

normalClause :: [Pat] -> Exp -> Clause
normalClause p e = Clause p (NormalB e) []
Expand Down Expand Up @@ -1992,7 +1994,6 @@ mkEntity embedEntityMap entityMap mps preDef = do
[keyFromRecordM'] <-
case unboundPrimarySpec entDef of
NaturalKey ucd -> do
recordVarName <- newName "record"
let
keyCon =
keyConName entDef
Expand All @@ -2002,15 +2003,11 @@ mkEntity embedEntityMap entityMap mps preDef = do
foldl'
AppE
(ConE keyCon)
(toList $ fmap
(\n ->
VarE n `AppE` VarE recordVarName
)
keyFields'
)
(VarE <$> keyFields')
keyFromRec = varP 'keyFromRecordM
lam = LamE [RecP name [(n, VarP n) | n <- toList keyFields']] constr
[d|
$(keyFromRec) = Just ( \ $(varP recordVarName) -> $(pure constr))
$(keyFromRec) = Just $(pure lam)
|]

_ ->
Expand Down
2 changes: 1 addition & 1 deletion persistent/persistent.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent
version: 2.14.4.3
version: 2.14.4.4
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand Down
3 changes: 3 additions & 0 deletions persistent/test/Database/Persist/TH/NoFieldSelectorsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import TemplateTestImports

mkPersist sqlSettings {mpsFieldLabelModifier = const id} [persistLowerCase|
User
ident Text
name Text
Primary ident
team TeamId

Team
name Text
Expand Down

0 comments on commit bea2018

Please sign in to comment.