Skip to content

Postgres: INSERT ON CONFLICT DO UPDATE with WHERE clause #11656

Description

@aheuermann

Issue Description

I'd like to be able to include a where clause in the a postgres upsert INSERT ON CONFLICT DO UPDATE statement. Something like:

INSERT INTO 
  person (name, last_action_tsp) 
  values ('steve', '2019-11-06T15:41:47.249Z') 
ON CONFLICT DO UPDATE 
  SET name=EXCLUDED.name, last_action_tsp=EXCLUDED.last_action_tsp 
WHERE 
   EXCLUDED.last_action_tsp > last_action_tsp

Is your feature request related to a problem? Please describe.

We only want to upsert if the timestamp (last_action_tsp) is greater than the value currently in the database.

Describe the solution you'd like

I think an api like this could work but open to suggestions.

Model.bulkCreate(data, {
  updateOnDuplicate: {
    columns: ['name', 'last_action_tsp'],
    where: {
      lastActionTsp: {
        [Op.lt]: literal('EXCLUDED.last_action_tsp'),
      },
    },
  },
});

Why should this be in Sequelize

This should be in sequelize since it is a supported by postgres but it looks like it may not be supported by mysql.

Describe alternatives/workarounds you've considered

The only workaround I can think of is to run an insert followed by an update with the condition but this results in two queries when ideally this could be done in one.

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): postgres

Would you be willing to resolve this issue by submitting a Pull Request?

I implemented this based on a pervious PR inDream#4
I know the merged implementation is different than the one proposed, but I am comfortable diving in and implementing this if you are open to it.

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. changed the title [-]updateOnDuplicate with where clause[/-] [+]Postgres: INSERT ON CONFLICT DO UPDATE with WHERE clause[/+] on Nov 6, 2019
  2. papb commented on Jan 15, 2020

    @papb
    Member

    Hello, sorry to take long to reply to your issue; this looks like a good think to exist...

    Hi @thaddeusmt, sorry to bother you, what do you think of this? Can you give an opinion? Thanks :)

  3. added
    dialect: postgres
    status: awaiting response
    type: feature
    on Jan 15, 2020
  4. added 4 commits that reference this issue on Mar 29, 2021
    ee94a9c
    ce7eefc
    2946739
    a8177b3
  5. github-actions commented on Nov 5, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  6. AnthonyRoss commented on Jun 1, 2022

    @AnthonyRoss

    Any update on this issue? Do we need to open a PR for this to get the feature added?

  7. removed
    status: awaiting response
    on Jun 1, 2022
  8. Rochet2 commented on Sep 2, 2022

    @Rochet2

    This would be useful for me for the exact same use case mentioned in the issue description.

    Seems that there is already a PR for this, but it was automatically closed as stale.

  9. clarencedesmarais commented on Sep 12, 2022

    @clarencedesmarais

    Very useful to me too. :)

  10. IslamWahid commented on Nov 29, 2023

    @IslamWahid

    needed here as well!

  11. Ferido07 commented on Jan 14, 2025

    @Ferido07

    I used conflictWhere like

            conflictWhere: {
                deletedAt: null,
            }
    

    on sequelize version 6.33.0 and it worked. Although it is not documented in https://sequelize.org/api/v6/class/src/model.js~model#static-method-bulkCreate. I got the information from https://sequelize.org/api/v7/interfaces/_sequelize_core.index.bulkcreateoptions#conflictWhere which is for version 7 but works on version 6.33 as well.

  12. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgrestype: feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions