-
Notifications
You must be signed in to change notification settings - Fork 301
Foreign key improvements #1121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
parsonsmatt
merged 8 commits into
yesodweb:foreign-key-improvements
from
kderme:foreign-key-improvements
Oct 30, 2020
Merged
Foreign key improvements #1121
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef41853
Simplify type check
kderme 488fe2d
Allow to reference implicit Primary Keys
kderme b8f8a2d
Parse References keyword after explicit Foreign Key
kderme 48618e9
Use explicit parent fields references
kderme 5a5213b
Use Cascade options on simple field references
kderme 44ca572
Support Cascade options for SQLite
kderme 7460dee
Update Changelog and add `@since`
kderme f7db477
Test nullable self reference
kderme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -290,7 +290,7 @@ data FieldSqlTypeExp = FieldSqlTypeExp FieldDef SqlTypeExp | |||||
|
|
||||||
| instance Lift FieldSqlTypeExp where | ||||||
| lift (FieldSqlTypeExp FieldDef{..} sqlTypeExp) = | ||||||
| [|FieldDef fieldHaskell fieldDB fieldType $(lift sqlTypeExp) fieldAttrs fieldStrict fieldReference fieldComments|] | ||||||
| [|FieldDef fieldHaskell fieldDB fieldType $(lift sqlTypeExp) fieldAttrs fieldStrict fieldReference fieldComments fieldCascadeOpts|] | ||||||
| #if MIN_VERSION_template_haskell(2,16,0) | ||||||
| liftTyped = unsafeTExpCoerce . lift | ||||||
| #endif | ||||||
|
|
@@ -1104,11 +1104,11 @@ mkEntity entityMap mps t = do | |||||
| fpv <- mkFromPersistValues mps t | ||||||
| utv <- mkUniqueToValues $ entityUniques t | ||||||
| puk <- mkUniqueKeys t | ||||||
| let primaryField = entityId t | ||||||
| fields <- mapM (mkField mps t) $ primaryField : entityFields t | ||||||
| fkc <- mapM (mkForeignKeysComposite mps t) $ entityForeigns t | ||||||
|
|
||||||
| let primaryField = entityId t | ||||||
|
|
||||||
| fields <- mapM (mkField mps t) $ primaryField : entityFields t | ||||||
| toFieldNames <- mkToFieldNames $ entityUniques t | ||||||
|
|
||||||
| (keyTypeDec, keyInstanceDecs) <- mkKeyTypeDec mps t | ||||||
|
|
@@ -1335,16 +1335,21 @@ mkLenses mps ent = fmap mconcat $ forM (entityFields ent) $ \field -> do | |||||
| ] | ||||||
|
|
||||||
| mkForeignKeysComposite :: MkPersistSettings -> EntityDef -> ForeignDef -> Q [Dec] | ||||||
| mkForeignKeysComposite mps t ForeignDef {..} = do | ||||||
| mkForeignKeysComposite mps t ForeignDef {..} = | ||||||
| if not foreignToPrimary then return [] else do | ||||||
| let fieldName f = mkName $ unpack $ recName mps (entityHaskell t) f | ||||||
| let fname = fieldName foreignConstraintNameHaskell | ||||||
| let reftableString = unpack $ unHaskellName foreignRefTableHaskell | ||||||
| let reftableKeyName = mkName $ reftableString `mappend` "Key" | ||||||
| let tablename = mkName $ unpack $ entityText t | ||||||
| recordName <- newName "record" | ||||||
|
|
||||||
| let fldsE = map (\((foreignName, _),_) -> VarE (fieldName foreignName) | ||||||
| `AppE` VarE recordName) foreignFields | ||||||
| let mkFldE ((foreignName, _),ff) = case ff of | ||||||
| (HaskellName {unHaskellName = "Id"}, DBName {unDBName = "id"}) | ||||||
| -> AppE (VarE $ mkName "toBackendKey") $ | ||||||
| VarE (fieldName foreignName) `AppE` VarE recordName | ||||||
| _ -> VarE (fieldName foreignName) `AppE` VarE recordName | ||||||
| let fldsE = map mkFldE foreignFields | ||||||
| let mkKeyE = foldl' AppE (maybeExp foreignNullable $ ConE reftableKeyName) fldsE | ||||||
| let fn = FunD fname [normalClause [VarP recordName] mkKeyE] | ||||||
|
|
||||||
|
|
@@ -1689,18 +1694,22 @@ liftAndFixKeys entityMap EntityDef{..} = | |||||
| |] | ||||||
|
|
||||||
| liftAndFixKey :: EntityMap -> FieldDef -> Q Exp | ||||||
| liftAndFixKey entityMap (FieldDef a b c sqlTyp e f fieldRef mcomments) = | ||||||
| [|FieldDef a b c $(sqlTyp') e f fieldRef' mcomments|] | ||||||
| liftAndFixKey entityMap (FieldDef a b c sqlTyp e f fieldRef mcomments casc) = | ||||||
| [|FieldDef a b c $(sqlTyp') e f fieldRef' mcomments casc|] | ||||||
| where | ||||||
| (fieldRef', sqlTyp') = fromMaybe (fieldRef, lift sqlTyp) $ | ||||||
| case fieldRef of | ||||||
| ForeignRef refName _ft -> case M.lookup refName entityMap of | ||||||
| Nothing -> Nothing | ||||||
| Nothing -> checkCascade | ||||||
| Just ent -> | ||||||
| case fieldReference $ entityId ent of | ||||||
| fr@(ForeignRef _Name ft) -> Just (fr, lift $ SqlTypeExp ft) | ||||||
| _ -> Nothing | ||||||
| _ -> Nothing | ||||||
| _ -> checkCascade | ||||||
| _ -> checkCascade | ||||||
| checkCascade = case casc of | ||||||
| FieldCascade Nothing Nothing -> Nothing | ||||||
| _ -> error $ "cascade field is not allown for field " <> show a | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
liftAndFixKey ... casc) = do
(fieldRef', sqlTyp') <- fromMaybe (fieldRef, lift sqlTyp) <$> checkCascade
[|FieldDef
where
checkCascade casc =
case fieldRef of
ForeignRef refName _ft -> ...
Nothing ->
case casc of ... |
||||||
| <> ". It doesn't reference any other tables." | ||||||
|
|
||||||
| deriving instance Lift EntityDef | ||||||
|
|
||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this TODO still relevant?