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.

Is ON CONFLICT DO UPDATE; possible?

See original GitHub issue

Was reading up on how ON CONFLICT DO NOTHING; works in the PostgreSQL docs (which is a great addition), and saw that ON CONFLICT DO UPDATE; is also an option, which is effectively an “upsert.”

I wonder if that’s also useful in a bulk loading context?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
igncampacommented, Aug 24, 2018

I like the way Django Postgres Extras handles it, but I believe copying that functionality would require some mayor rewriting of how Django Postgres Copy works.

A very rudimentary solution I propose is passing a list with the following order:

  1. Operation (DO NOTHING or DO UPDATE)
  2. Constraint (the name of the column with a constraint)
  3. Columns to update (in the case of DO UPDATE)

I replaced ignore_conflicts with on_conflict and defaults to an empty list:

def __init__(
    ...
    on_conflict=[],
    ...
):

def insert_suffix(self):
    # If on_conflict is not an empty list
    if self.on_conflict:
        # First item on list - Operation
        if self.on_conflict[0] == 'DO NOTHING':
            return """
                ON CONFLICT DO NOTHING;
            """
        elif self.on_conflict[0] == 'DO UPDATE':
            # Second item on list - Constraint
            constraint = self.on_conflict[1]
            # Delete first two items on list. Only columns to be updated remain
            del self.on_conflict[0:2]
            update_columns =
                ', '.join(["{0} = EXCLUDED.{0}".format(col) for col in self.on_conflict])
            return """
                ON CONFLICT ({0}) DO UPDATE SET {1};
                """.format(constraint, update_columns)
    else:
        return ";"

I tested it out and it seems to work well. I understand it’s not the most pythonic solution though

an example would be: Customers.objects.from_csv('/path/to/file.csv', on_conflict=['DO UPDATE', 'name', 'email']) with multiple fields to update: Customers.objects.from_csv('/path/to/file.csv', on_conflict=['DO UPDATE', 'name', 'email', 'active', 'age']) or in plain english: if field on column 'name' already exists, update 'email', 'active' and 'age' fields

1reaction
timmyGrcommented, Aug 29, 2018

Thanks a ton of the snippet. We forked the project and got everything going.

We also added temp_table_name_suffix because this will be used in Airflow with Celery, and also added static_mapping_on_conflict to meet our needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use `INSERT ON CONFLICT` to upsert data in ... - Prisma
Using the DO UPDATE action. If, instead, we want to update rows when they already exist in the table, we can use the...
Read more >
PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all ...
It can be either DO NOTHING, or a DO UPDATE clause specifying the exact details of the UPDATE action to be performed in...
Read more >
PostgreSQL Upsert Using INSERT ON CONFLICT statement
This tutorial shows you how to use the PostgreSQL upsert feature to insert or update data if the row that is being inserted...
Read more >
Use INSERT ON CONFLICT to overwrite data - AnalyticDB for ...
The INSERT ON CONFLICT statement allows you to update an existing row that contains a primary key when you execute the INSERT statement...
Read more >
INSERT .. ON CONFLICT DO UPDATE SET error
Last_seen_tmp,Duration=tmp... And even if I try to use the special table EXCLUDED that references to the possible inserted values with the ...
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