-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is ON CONFLICT DO UPDATE; possible? #91
Comments
Has Django tackled this yet? |
Not sure if at the database level, but it's essentially what |
I don't think Which I find a bit conflicting since the amount of possible logic combinations becomes very large; now the user can choose any combination of expressions, default values and sub-queries per every column to be updated, plus a where condition to all that logic. I definitely think it's a great addition. I just wonder if we'd be better off simply leaving an empty slot to which the user can add its own update logic string, which is an idea I also dislike. |
I would love to see this feature. We import about 11 million records a day and Dango Postgres Extras currently handles upserts. https://django-postgres-extra.readthedocs.io/manager/#upserting |
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:
I replaced
I tested it out and it seems to work well. I understand it's not the most pythonic solution though an example would be: |
Thanks a ton of the snippet. We forked the project and got everything going. We also added |
Wow. I'm impressed. You guys are flying by my understanding of If it is possible, I'd prefer to adopt a Pythonic system using Django idioms or proven techniques from other Python libraries like psycopg2. If something like that exists and you guys think it will work, let me know. If not, perhaps in the meantime we could extend the private functions inside this library so that ones that tack together the raw SQL can be extended by creative users such as yourself. |
Here's a look at how django-postgres-extra handles their It seems that extending Django's functionality is the best practice, however django-postgres-copy went a different route from the beginning and it would require a major rewrite of the library to subclass from the SQLCompiler. I'm definitely not an expert in the matter. The above is simply my impression after trying to figure this out. |
Any chance this is on the roadmap? Looks like it would be a great addition to the lib. |
I'm putting together a PR that addresses this as well as #122; I believe my approach is sufficient to avoid concerns with raw SQL being passed as args/kwargs. I've got the core of the code working well although I need to add tests (admittedly, not my strongest suit). @palewire Since 2.7.2 requires Django 4, does that mean you're okay with Python 3.8 coding, e.g. walrus operator, or would you prefer those left out? Hoping to submit the PR by Sunday or Monday. |
This feature would be great for my use case too : keeping imported data up-to-date with the source (daily synchronization). I would not use a An optional |
Or maybe a simple
What do you think? |
@ipamo What about a simple I designed my original PR to mirror the actual logic of the @palewire Any thoughts? |
Using
The last point is obviously a quite personal reason but I prefer to state it as others might have the same impression. Also, I think we need an easy way to specify the key (if there are several unique constraints) and the updated columns. Hence I also like the idea of |
Regarding |
Idea: using suffix in dict key": Use case 1:
Use case 2:
Use case 3:
|
Well I realize it's probably too complex. We would have to document clearly that:
Let me know what you think. |
I think your concepts dealing with I feel like using an Furthermore, while I understand your hesitation of using labels ('update' or 'ignore'), if we used e.g. booleans, then it would be possible to have |
I understand your points too, even if I stay with my personal preference ;) How would you deal with specifying the update key / columns ? |
Well, my PR #156 uses a dictionary such as
This follows the convention for the |
Was reading up on how
ON CONFLICT DO NOTHING;
works in the PostgreSQL docs (which is a great addition), and saw thatON CONFLICT DO UPDATE;
is also an option, which is effectively an "upsert."I wonder if that's also useful in a bulk loading context?
The text was updated successfully, but these errors were encountered: