Skip to content

Bulk UPSERT Confusion #1118

@allochi

Description

@allochi

Hi,

I'm sorry if this is not the place to ask this question, I wouldn't have if there was a section about it in the documentation.

I'm trying to use Bulk UPSERT with a simple table

create table items (
	id serial primary key,
	name text
);

I then insert these records

curl -X "POST" "http://localhost:5200/items" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'[
  {
    "name": "iPad"
  },
  {
    "name": "iPhone"
  },
  {
    "name": "MacBook Pro"
  }
]'

When I try to UPSERT with this

## Upsert Items
curl -X "POST" "http://localhost:5200/items" \
     -H 'Prefer: resolution=merge-duplicates' \
     -H 'Content-Type: application/json' \
     -d $'[
  {
    "id": 1,
    "name": "iPad"
  },
  {
    "id": 2,
    "name": "iPhone"
  },
  {
    "id": 3,
    "name": "Macbook Pro"
  },
  {
    "id": null,
    "name": "PostgREST"
  }
]'
  1. if I set id to null in the last item, I get error 400 null value in column "id" violates not-null constraint
  2. if I don't include id in the last item, I get error 400 "message":"All object keys must match"

The only way I can get this to work is by providing a value for id, which I can't as id is auto-generated from a sequence, isn't the purpose of UPSERT is to INSERT a new record and in case of conflict with the primary key either update or ignore? how can I insert a new record in this case?

Could you please advice on how this suppose to work? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions