question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docs: document upserts which "do nothing" to return row

See original GitHub issue

Right now when you use an upsert mutation and there is a conflict, you’re unable to return the row. For example:

mutation update_participants {
  insert_participants(objects: {user_id: "1"}, on_conflict: {constraint: participants_user_id_sprint_token_key, update_columns: []}) {
    returning {
      id
      user_id
    }
  }
}

Will return:

{
  "data": {
    "insert_participants": {
      "returning": []
    }
  }
}

Instead, what it should return:

{
  "data": {
    "insert_participants": {
      "returning": [{
        id: 1,
        user_id: 1
      }]
    }
  }
}

Right now if you want to mimic this behavior you need to run a query to see if a record exists followed by a mutation to create if it doesn’t.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
0x777commented, May 27, 2019

@timminkov Unfortunately empty update_columns translates to Postgres’s on conflict do nothing and that doesn’t return any results. So a simple work around is to update one of the columns from the unique constraint (update_columns: [user_id]).

0reactions
colemerrickcommented, Mar 23, 2022

@timminkov Unfortunately empty update_columns translates to Postgres’s on conflict do nothing and that doesn’t return any results. So a simple work around is to update one of the columns from the unique constraint (update_columns: [user_id]).

This forces you to set some bizarre permissions on a db level ie the least dangerous column to update in your unique constraint. Therefore, we really need a real upsert mutation - not an insert on conflict: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/upsert.html.

Read more comments on GitHub >

github_iconTop Results From Across the Web

query of type "INSERT ON CONFLICT DO NOTHING ...
The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted (or updated, if an ON CONFLICT...
Read more >
Upsert in SQL: What is an upsert, and when should you use ...
In this example, the primary key value of 2 already exists in the table, so the UPSERT operation updated that row with the...
Read more >
Why doesn't the insert/upsert rows endpoint return addedRowIds
Why doesn't the insert/upsert rows API endpoint return addedRowIds when keyColumns is not set ? This behaviour is documented, but it' odd.
Read more >
UPSERT - PostgreSQL wiki
Committed version's INSERT documentation: ... UPDATE that existing row instead, while safely giving little to no further thought to ...
Read more >
Timescale Documentation | Upsert data
Either updating the existing row, or doing nothing, if a matching row already exists. Upserts only work when you have a unique index...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found