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

Bulk insert with only one record fails when no primary keys specified in select= #1087

Closed
gavrilyak opened this issue Mar 29, 2018 · 2 comments
Labels

Comments

@gavrilyak
Copy link

postgrest fails with cryptic message when only one record is posted in bulk insert (or simple insert) and select parameter does not ask for primary key.
When more then one record posted then everything works fine.
As I understood the problem is with headers field, database returns null in this case and with

              let (isSingle, nRows) = case pjType of
                                        PJArray len -> (len == 1, len)

In this case postgres unexpectedly wants to return location header and fails.

Simple test scenario snippet follows

$ psql -c 'CREATE TABLE test_bulk(id serial primary key, code text)'

$ curl -X POST -d '{"code":42}'  -H 'Prefer:return=representation' -H "Content-type: application/json" -H http://localhost:3000/test_bulk?select=code
{"details":"Row number 0","message":"Row error: unexpected null"}

$ curl -X POST -d '[{"code":42}]'  -H 'Prefer:return=representation' -H "Content-type: application/json" -H http://localhost:3000/test_bulk?select=code
{"details":"Row number 0","message":"Row error: unexpected null"}

$ curl -X POST -d '[{"code":42}]'  -H 'Prefer:return=representation' -H "Content-type: application/json" -H  http://localhost:3000/test_bulk?select=id,code
[{"id":18,"code":"42"}]

$ curl -X POST -d '[{"code":42},{"code":43}]'  -H 'Prefer:return=representation' -H "Content-type: application/json"  http://localhost:3000/test_bulk?select=code
[{"code":"42"},{"code":"43"}]

Version: current stable 4.4.0 from docker hub

@steve-chavez
Copy link
Member

The test case doesn't fail on master:

curl -v -d '{"code":42}' \
-H 'Prefer:return=representation' \
-H "Content-type: application/json" \
http://localhost:3000/test_bulk?select=code
# Server: postgrest/0.4.4.0 (32c7e32)
# Content-Type: application/json; charset=utf-8
# Content-Range: */*
# Gives: [{"code":"42"}]
# No Location header present

This is because of this coalesce that was introduced in #1048.

A problem that comes with that change is that the Location header will be missing for successful POSTs and it shouldn't, I think the best solution for this issue would be making the Location header independent of the select.

@steve-chavez
Copy link
Member

There's no error on the latest version, but the Location header is missing, I've opened a new issue #1162 for this, now is more of a minor bug.

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

2 participants