Is ON CONFLICT DO UPDATE; possible?
See original GitHub issueWas 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:
- Created 5 years ago
- Reactions:1
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
DO NOTHING
orDO UPDATE
)DO UPDATE
)I replaced
ignore_conflicts
withon_conflict
and defaults to an empty list: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
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 addedstatic_mapping_on_conflict
to meet our needs.