From 1b12b112a1e491f51e48e37d5d828e4a390de26b Mon Sep 17 00:00:00 2001 From: laurenceisla Date: Mon, 31 May 2021 12:15:31 -0500 Subject: [PATCH] fix: Add a hint and clarification to the no relationship found error (#1858) --- src/PostgREST/Error.hs | 3 ++- test/Feature/EmbedDisambiguationSpec.hs | 12 +++++++++--- test/Feature/QuerySpec.hs | 6 ++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/PostgREST/Error.hs b/src/PostgREST/Error.hs index 2712e269c..690acdc96 100644 --- a/src/PostgREST/Error.hs +++ b/src/PostgREST/Error.hs @@ -86,7 +86,8 @@ instance JSON.ToJSON ApiRequestError where toJSON InvalidRange = JSON.object [ "message" .= ("HTTP Range error" :: Text)] toJSON (NoRelBetween parent child) = JSON.object [ - "message" .= ("Could not find foreign keys between these entities. No relationship found between " <> parent <> " and " <> child :: Text)] + "hint" .= ("If a new foreign key between these entities was created in the database, try reloading the schema cache." :: Text), + "message" .= ("Could not find a relationship between " <> parent <> " and " <> child <> " in the schema cache" :: Text)] toJSON (AmbiguousRelBetween parent child rels) = JSON.object [ "hint" .= ("By following the 'details' key, disambiguate the request by changing the url to /origin?select=relationship(*) or /origin?select=target!relationship(*)" :: Text), "message" .= ("More than one relationship was found for " <> parent <> " and " <> child :: Text), diff --git a/test/Feature/EmbedDisambiguationSpec.hs b/test/Feature/EmbedDisambiguationSpec.hs index 859d1a326..9db3c3ead 100644 --- a/test/Feature/EmbedDisambiguationSpec.hs +++ b/test/Feature/EmbedDisambiguationSpec.hs @@ -184,7 +184,9 @@ spec = it "fails if the fk is not known" $ get "/message?select=id,sender:person!space(name)&id=lt.4" `shouldRespondWith` - [json|{"message":"Could not find foreign keys between these entities. No relationship found between message and person"}|] + [json|{ + "hint":"If a new foreign key between these entities was created in the database, try reloading the schema cache.", + "message":"Could not find a relationship between message and person in the schema cache"}|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] } @@ -443,13 +445,17 @@ spec = { matchHeaders = [matchContentTypeJson] } it "doesn't work if the junction is only internal" $ get "/end_1?select=end_2(*)" `shouldRespondWith` - [json|{"message":"Could not find foreign keys between these entities. No relationship found between end_1 and end_2"}|] + [json|{ + "hint":"If a new foreign key between these entities was created in the database, try reloading the schema cache.", + "message":"Could not find a relationship between end_1 and end_2 in the schema cache"}|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] } it "shouldn't try to embed if the private junction has an exposed homonym" $ -- ensures the "invalid reference to FROM-clause entry for table "rollen" error doesn't happen. -- Ref: https://github.com/PostgREST/postgrest/issues/1587#issuecomment-734995669 get "/schauspieler?select=filme(*)" `shouldRespondWith` - [json|{"message":"Could not find foreign keys between these entities. No relationship found between schauspieler and filme"}|] + [json|{ + "hint":"If a new foreign key between these entities was created in the database, try reloading the schema cache.", + "message":"Could not find a relationship between schauspieler and filme in the schema cache"}|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] } diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 80826f3d4..97b88d131 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -225,8 +225,10 @@ spec actualPgVersion = do get "/items?always_false=is.false" `shouldRespondWith` 400 it "matches filtering nested items 2" $ - get "/clients?select=id,projects(id,tasks2(id,name))&projects.tasks.name=like.Design*" - `shouldRespondWith` [json| {"message":"Could not find foreign keys between these entities. No relationship found between projects and tasks2"}|] + get "/clients?select=id,projects(id,tasks2(id,name))&projects.tasks.name=like.Design*" `shouldRespondWith` + [json| { + "hint":"If a new foreign key between these entities was created in the database, try reloading the schema cache.", + "message":"Could not find a relationship between projects and tasks2 in the schema cache"}|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] }