diff --git a/src/PostgREST/ApiRequest/Types.hs b/src/PostgREST/ApiRequest/Types.hs index cbc1231ca..e419dae4a 100644 --- a/src/PostgREST/ApiRequest/Types.hs +++ b/src/PostgREST/ApiRequest/Types.hs @@ -71,13 +71,13 @@ data ApiRequestError | InvalidRpcMethod ByteString | LimitNoOrderError | NotFound - | NotToOne Text Text | NoRelBetween Text Text Text | NoRpc Text Text [Text] Bool MediaType Bool | NotEmbedded Text | ParseRequestError Text Text | PutRangeNotAllowedError | QueryParamError QPError + | RelatedOrderNotToOne Text Text | SpreadNotToOne Text Text | UnacceptableSchema [Text] | UnsupportedMethod ByteString diff --git a/src/PostgREST/Error.hs b/src/PostgREST/Error.hs index caad95b38..07accc74b 100644 --- a/src/PostgREST/Error.hs +++ b/src/PostgREST/Error.hs @@ -64,7 +64,6 @@ instance PgrstError ApiRequestError where status InvalidRpcMethod{} = HTTP.status405 status InvalidRange{} = HTTP.status416 status NotFound = HTTP.status404 - status NotToOne{} = HTTP.status400 status NoRelBetween{} = HTTP.status400 status NoRpc{} = HTTP.status404 @@ -72,6 +71,7 @@ instance PgrstError ApiRequestError where status ParseRequestError{} = HTTP.status400 status PutRangeNotAllowedError = HTTP.status400 status QueryParamError{} = HTTP.status400 + status RelatedOrderNotToOne{} = HTTP.status400 status SpreadNotToOne{} = HTTP.status400 status UnacceptableSchema{} = HTTP.status406 status UnsupportedMethod{} = HTTP.status405 @@ -154,10 +154,10 @@ instance JSON.ToJSON ApiRequestError where "details" .= JSON.Null, "hint" .= JSON.Null] - toJSON (NotToOne origin target) = JSON.object [ + toJSON (RelatedOrderNotToOne origin target) = JSON.object [ "code" .= ApiRequestErrorCode18, - "message" .= ("'" <> origin <> "' and '" <> target <> "' do not form a many-to-one or one-to-one relationship" :: Text), - "details" .= JSON.Null, + "message" .= ("A related order on '" <> target <> "' is not possible" :: Text), + "details" .= ("'" <> origin <> "' and '" <> target <> "' do not form a many-to-one or one-to-one relationship" :: Text), "hint" .= JSON.Null] toJSON (SpreadNotToOne origin target) = JSON.object [ diff --git a/src/PostgREST/Plan.hs b/src/PostgREST/Plan.hs index a79acace5..54c69f103 100644 --- a/src/PostgREST/Plan.hs +++ b/src/PostgREST/Plan.hs @@ -328,7 +328,7 @@ addRelatedOrders (Node rp@ReadPlan{order,from} forest) = do name = fromMaybe relName relAlias in if isToOne == Just True then Right $ ot{otRelation=relAggAlias} - else Left $ NotToOne (qiName from) name + else Left $ RelatedOrderNotToOne (qiName from) name Nothing -> Left $ NotEmbedded otRelation diff --git a/test/spec/Feature/Query/RelatedQueriesSpec.hs b/test/spec/Feature/Query/RelatedQueriesSpec.hs index ddb35c377..0d3167415 100644 --- a/test/spec/Feature/Query/RelatedQueriesSpec.hs +++ b/test/spec/Feature/Query/RelatedQueriesSpec.hs @@ -107,17 +107,32 @@ spec = it "fails when is not a to-one relationship" $ do get "/clients?select=*,projects(*)&order=projects(id)" `shouldRespondWith` - [json|{"code":"PGRST118","details":null,"hint":null,"message":"'clients' and 'projects' do not form a many-to-one or one-to-one relationship"}|] + [json|{ + "code":"PGRST118", + "details":"'clients' and 'projects' do not form a many-to-one or one-to-one relationship", + "hint":null, + "message":"A related order on 'projects' is not possible" + }|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] } get "/clients?select=*,pros:projects(*)&order=pros(id)" `shouldRespondWith` - [json|{"code":"PGRST118","details":null,"hint":null,"message":"'clients' and 'pros' do not form a many-to-one or one-to-one relationship"}|] + [json|{ + "code":"PGRST118", + "details":"'clients' and 'pros' do not form a many-to-one or one-to-one relationship", + "hint":null, + "message":"A related order on 'pros' is not possible" + }|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] } get "/designers?select=id,computed_videogames(id)&order=computed_videogames(id).desc" `shouldRespondWith` - [json|{"code":"PGRST118","details":null,"hint":null,"message":"'designers' and 'computed_videogames' do not form a many-to-one or one-to-one relationship"}|] + [json|{ + "code":"PGRST118", + "details":"'designers' and 'computed_videogames' do not form a many-to-one or one-to-one relationship", + "hint":null, + "message":"A related order on 'computed_videogames' is not possible" + }|] { matchStatus = 400 , matchHeaders = [matchContentTypeJson] }