Skip to content

Commit 56c1447

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
fix: insert with missing=default uses column default before using domain default
1 parent 0d64044 commit 56c1447

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2525
- #3727, Clarify "listening" logs - @steve-chavez
2626
- #3795, Clarify `Accept: vnd.pgrst.object` error message - @steve-chavez
2727
- #3779, Always log the schema cache load time - @steve-chavez
28+
- #3706, Fix insert with `missing=default` uses default value of domain instead of column - @taimoorzaeem
2829

2930
### Changed
3031

src/PostgREST/SchemaCache.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ tablesSqlQuery =
622622
d.description AS description,
623623
-- typbasetype and typdefaultbin handles `CREATE DOMAIN .. DEFAULT val`, attidentity/attgenerated handles generated columns, pg_get_expr gets the default of a column
624624
CASE
625-
WHEN t.typbasetype != 0 THEN pg_get_expr(t.typdefaultbin, 0)
625+
WHEN (t.typbasetype != 0) AND (ad.adbin IS NULL) THEN pg_get_expr(t.typdefaultbin, 0)
626626
WHEN a.attidentity = 'd' THEN format('nextval(%L)', seq.objid::regclass)
627627
WHEN a.attgenerated = 's' THEN null
628628
ELSE pg_get_expr(ad.adbin, ad.adrelid)::text

test/spec/Feature/Query/InsertSpec.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,15 @@ spec actualPgVersion = do
572572
, matchHeaders = ["Preference-Applied" <:> "missing=default, return=representation"]
573573
}
574574

575+
it "inserts a COLUMN default before a DOMAIN default with missing=default" $
576+
request methodPost "/evil_friends_with_column_default?columns=id,name" [("Prefer", "return=representation"), ("Prefer", "missing=default")]
577+
[json| { "name": "Demon" } |]
578+
`shouldRespondWith`
579+
[json| [{"id": 420, "name": "Demon"}] |]
580+
{ matchStatus = 201
581+
, matchHeaders = ["Preference-Applied" <:> "missing=default, return=representation"]
582+
}
583+
575584
it "inserts json that has duplicate keys" $ do
576585
request methodPost "/tbl_w_json" [("Prefer", "return=representation")]
577586
[json| { "data": { "a": 1, "a": 2 }, "id": 3 } |]

test/spec/fixtures/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,6 +3274,11 @@ create table evil_friends(
32743274
, name text
32753275
);
32763276

3277+
create table evil_friends_with_column_default(
3278+
id devil_int default 420
3279+
, name text
3280+
);
3281+
32773282
create table bets (
32783283
id int
32793284
, data_json json

0 commit comments

Comments
 (0)