Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: handle column default types correctly in SchemaCache #3750

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 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.

Comment on lines +7 to 9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a Fixed h3-section under the Unreleased h2-section. It should be added to that section. Also you are still not following the format as the other log entries below. Have a close look where the issue number is placed, note that the author is listed at the end, etc...

### Added

Expand Down Expand Up @@ -1095,3 +1096,6 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Make filter position match docs, e.g. `?order=col.asc` rather
than `?order=asc.col`.



10 changes: 6 additions & 4 deletions src/PostgREST/SchemaCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +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
ELSE pg_get_expr(ad.adbin, ad.adrelid)::text
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"]
}

Comment on lines +566 to +575
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laurence asked you to write a test-case similar to this one, but with some changes (i.e. test for column default, not domain default)...

Instead of adjusting the test-case, so that it actually matches what you are trying to achieve.. you just copy-pasted the exact same thing, which is a duplicate of the test-case right after.

What are you trying to achieve?

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
Loading