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

Doing a bulk insert with missing=default uses the default value of the domain instead of the column #3706

Open
laurenceisla opened this issue Aug 26, 2024 · 0 comments
Labels

Comments

@laurenceisla
Copy link
Member

Environment

  • PostgreSQL version: 15.7
  • PostgREST version: 12.2.3
  • Operating system: NixOS

Description of issue

As the title says. According to the PostgreSQL docs:

The default expression will be used in any insert operation that does not specify a value for the column. If a default value is defined for a particular column, it overrides any default associated with the domain. In turn, the domain default overrides any default value associated with the underlying data type.

To reproduce this:

create domain defdom as integer default 1;

create table test_defdom (
  id int primary key,
  val api.defdom default 10
);
curl -X POST 'localhost:3000/test_defdom?columns=id,val' \
  -H 'Content-Type: application/json' \
  -H 'Prefer: missing=default, return=representation' \
  -d '[{"id": 1}]'

Expected:

[{"id":1,"val":10}]

Got:

[{"id":1,"val":1}]

Solution

Check if the defaults of the columns are not null first, otherwise use the domain default instead.

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
END AS column_default,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants
@laurenceisla and others