fix: Location header with a null PK on a view (#1875)

Regression after #1475
This commit is contained in:
laurenceisla
2021-06-18 18:09:19 -05:00
committed by GitHub
parent 1b12b112a1
commit 59e0b006a5
4 changed files with 35 additions and 2 deletions
+1 -1
View File
@@ -172,7 +172,7 @@ asBinaryF fieldName = "coalesce(string_agg(_postgrest_t." <> pgFmtIdent fieldNam
locationF :: [Text] -> SqlFragment
locationF pKeys = [qc|(
WITH data AS (SELECT row_to_json(_) AS row FROM {sourceCTEName} AS _ LIMIT 1)
SELECT array_agg(json_data.key || '=eq.' || json_data.value)
SELECT array_agg(json_data.key || '=' || coalesce('eq.' || json_data.value, 'is.null'))
FROM data CROSS JOIN json_each_text(data.row) AS json_data
WHERE json_data.key IN ('{fmtPKeys}')
)|]
+11 -1
View File
@@ -527,7 +527,7 @@ spec actualPgVersion = do
, matchHeaders = [ matchHeaderAbsent hLocation ]
}
context "requesting header only representation" $
context "requesting header only representation" $ do
it "returns a location header" $
request methodPost "/compound_pk_view" [("Prefer", "return=headers-only")]
[json|{"k1":1,"k2":"test","extra":2}|]
@@ -537,3 +537,13 @@ spec actualPgVersion = do
, matchHeaders = [ "Location" <:> "/compound_pk_view?k1=eq.1&k2=eq.test"
, "Content-Range" <:> "*/*" ]
}
it "should not throw and return location header when a PK is null" $
request methodPost "/test_null_pk_competitors_sponsors" [("Prefer", "return=headers-only")]
[json|{"id":1}|]
`shouldRespondWith`
""
{ matchStatus = 201
, matchHeaders = [ "Location" <:> "/test_null_pk_competitors_sponsors?id=eq.1&sponsor_id=is.null"
, "Content-Range" <:> "*/*" ]
}
+1
View File
@@ -37,6 +37,7 @@ GRANT ALL ON TABLE
, projects
, projects_view
, projects_view_alt
, test_null_pk_competitors_sponsors
, simple_pk
, simple_pk2
, tasks
+22
View File
@@ -670,6 +670,28 @@ CREATE VIEW projects_view_alt AS
projects.client_id as t_client_id
FROM projects;
CREATE TABLE sponsors (
id integer primary key,
name varchar(20) not null
);
CREATE TABLE competitors (
id integer primary key,
sponsor_id integer references sponsors(id),
full_name varchar(40) not null
);
CREATE VIEW test_null_pk_competitors_sponsors AS
SELECT c.id, s.id as sponsor_id
FROM competitors c
LEFT JOIN sponsors s on c.sponsor_id = s.id;
CREATE RULE test_null_pk_competitors_sponsors AS
ON INSERT TO test_null_pk_competitors_sponsors DO INSTEAD
INSERT INTO competitors(id, full_name, sponsor_id)
VALUES (new.id, 'Competitor without sponsor', new.sponsor_id)
RETURNING id, sponsor_id;
--
-- Name: simple_pk; Type: TABLE; Schema: test; Owner: -
--