diff --git a/CHANGELOG.md b/CHANGELOG.md index 279591132d..86b470d74a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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. + diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index d2ba63cfa6..4543688e06 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -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 diff --git a/test/spec/Feature/Query/InsertSpec.hs b/test/spec/Feature/Query/InsertSpec.hs index 405ef62f9f..b478e99621 100644 --- a/test/spec/Feature/Query/InsertSpec.hs +++ b/test/spec/Feature/Query/InsertSpec.hs @@ -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" } |]