django-postgres-copy: Is ON CONFLICT DO UPDATE; possible?
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?
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 20 (6 by maintainers)
Commits related to this issue
- Address #91. — committed to geoffrey-eisenbarth/django-postgres-copy by geoffrey-eisenbarth a year ago
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 NOTHINGorDO UPDATE)DO UPDATE)I replaced
ignore_conflictswithon_conflictand 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' fieldsThanks a ton of the snippet. We forked the project and got everything going.
We also added
temp_table_name_suffixbecause this will be used in Airflow with Celery, and also addedstatic_mapping_on_conflictto meet our needs.I would love to see this feature. We import about 11 million records a day and
COPYis by far the fastest.Dango Postgres Extras currently handles upserts.
https://django-postgres-extra.readthedocs.io/manager/#upserting