Skip to content

Commit

Permalink
fix: Corrected handling of column default values in SchemaCache to ad…
Browse files Browse the repository at this point in the history
…dress issue #3706
  • Loading branch information
mursidx committed Oct 22, 2024
1 parent 229b110 commit 2c023d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed
- **fix**: Improved handling of column default values in `SchemaCache`. This addresses issue #3706 by ensuring all relevant cases are covered, including scenarios where `ad.adbin` is not NULL.

### Added

Expand Down Expand Up @@ -1097,5 +1098,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
than `?order=asc.col`.
### Unreleased
- **fix**: Improved handling of column default values in `SchemaCache`. This addresses issue #3706 by ensuring all relevant cases are covered, including scenarios where `ad.adbin` is not NULL.
11 changes: 6 additions & 5 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,12 @@ tablesSqlQuery =
d.description AS description,
-- typbasetype and typdefaultbin handles `CREATE DOMAIN .. DEFAULT val`, attidentity/attgenerated handles generated columns, pg_get_expr gets the default of a column
CASE
WHEN t.typbasetype != 0 THEN pg_get_expr(t.typdefaultbin, 0)
WHEN a.attidentity = 'd' THEN format('nextval(%L)', seq.objid::regclass)
WHEN a.attgenerated = 's' THEN null
WHEN ad.adbin IS NOT NULL THEN pg_get_expr(ad.adbin, ad.adrelid)::text
ELSE NULL -- Handle cases where none of the conditions are met
WHEN a.attdefault IS NOT NULL THEN pg_get_expr(a.attdefault, a.attrelid)::text -- Column default
WHEN t.typbasetype != 0 THEN pg_get_expr(t.typdefaultbin, 0) -- Domain default
WHEN a.attidentity = 'd' THEN format('nextval(%L)', seq.objid::regclass) -- Identity column
WHEN a.attgenerated = 's' THEN NULL -- Generated column
WHEN ad.adbin IS NOT NULL THEN pg_get_expr(ad.adbin, ad.adrelid)::text -- Other default expressions
ELSE NULL
END AS column_default,
not (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable,
CASE
Expand Down
10 changes: 10 additions & 0 deletions test/spec/Feature/Query/InsertSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,16 @@ spec actualPgVersion = do
}|])
{ matchStatus = 400 }

it "inserts a default on a DOMAIN with default" $
request methodPost "/evil_friends?columns=id,name"
[("Prefer", "return=representation"), ("Prefer", "missing=default")]
[json| { "name": "Lu" } |]
`shouldRespondWith`
[json| [{"id": 666, "name": "Lu"}] |]
{ matchStatus = 201
, matchHeaders = ["Preference-Applied" <:> "missing=default, return=representation"]
}

it "inserts a default on a DOMAIN with default" $
request methodPost "/evil_friends?columns=id,name" [("Prefer", "return=representation"), ("Prefer", "missing=default")]
[json| { "name": "Lu" } |]
Expand Down

0 comments on commit 2c023d5

Please sign in to comment.