-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Fixed #31685 -- Added support for updating conflicts to QuerySet.bulk_create(). #13065
Conversation
I just gave the patch a quick glance, but I am wondering if it wouldn't make more sense to have the argument similar to the |
It's a good idea. I'll change this argument to what you suggest. |
It was discussed in the original ticket-28668 and #9192, see comment. |
I would change |
Okay, I've changed upsert to update. I don' know it is a good way to doing like this or not. |
af2f8ff
to
0f92932
Compare
Thank you for writing this pull request. Looking forward to using this feature |
Hi @ChihSeanHsu I noticed you ticked Needs Tests and Needs Documentation on the ticket, which signals to reviewers this isn't ready to review. Be sure to untick those if you've got tests and docs and you're ready for a review. Also this would be a good time to resolve the merge conflict. Thanks! |
342a490
to
b42781d
Compare
Hi @jacobtylerwalls, |
Hi @ChihSeanHsu, great job! However, I think there is some room for improvement. |
@alextatarinov good point, we would need similar logic to what's used in django/django/db/models/base.py Lines 1037 to 1045 in 10f8b82
But as you say it would probably make more sense to let the user specify the fields to conflict on. |
Hi @alextatarinov , thanks for your suggestion, I'll add it to this pull request. |
Hi @alextatarinov and @hannseman, I have a question need to clarify, this new feature is based on And upsert function in this library is not using this SQL command, it controls this action in application level not database. |
Hello again, @ChihSeanHsu. If you specify P.S. I am nowhere an expert, so I suggest we ask someone more experienced. |
|
||
for unique_together in opts.unique_together: | ||
unique_fields.extend(unique_together) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also collect class based unique-constraints, you get can them from opts.total_unique_constraints
.
I'm not really sure that collecting all unique fields and then using them for For example: CREATE TABLE person(
email varchar UNIQUE,
phone varchar UNIQUE,
name varchar
); persons = [Person(name="Some name", phone="+46700000000", email="test@example.com")]
Person.objects.bulk_create(persons, update_conflicts=True, update_fields=["name"]) Would generate the following SQL: INSERT INTO person(email, phone, name) VALUES ('test@example.com', '+46700000000', 'Some name')
ON CONFLICT (email, phone)
DO UPDATE SET name = EXCLUDED.name; This would error with |
Hello again,
After I surveyed and as you mentioned, the
And I will add more unittest to cover this situation. |
d554d68
to
fe3e848
Compare
Not sure if it's my place to comment but... I wonder if the API could be made a bit more flexible by modeling a bit more like CREATE TABLE foo (
timestamp timestamp NOT NULL,
num_requests integer NOT NULL
); INSERT INTO
foo (timestamp, num_requests)
VALUES
('2020-12-21T12:00:00', 10)
ON CONFLICT
(timestamp)
DO UPDATE SET
num_requests = foo.num_requests + EXCLUDE.num_requests; foos = [
Foo(timestamp="2020-12-21T12:00:00", num_requests=F("num_requests") + 10),
]
Foo.objects.bulk_create(foos, update_conflicts=True, update_fields=["num_requests"]) Not sure how portable that is though, or if it's mostly a PostgreSQL thing. |
fe3e848
to
d8a6c77
Compare
Hi @lithammer, It's a good suggestion, but But if there is requirement, we can discuss it. |
Thanks for the continued work here. Is this ready for re-review? If so, please make sure to uncheck Needs Improvement on the ticket each time it's ready for re-review. (Would be helpful if you could also bump the release note and change annotations to 4.1). Cheers. |
ddd5ab3
to
0da99dd
Compare
Hi @jacobtylerwalls , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChihSeanHsu Thanks for updates 👍 I left more comment, however didn't go through docs and tests in detail. I'll do this in the next round.
Please add also release notes for the new feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChihSeanHsu Thanks 👍 I left more comment. Again I didn't go through docs and tests in detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChihSeanHsu Thanks for updates and all your efforts 👍 I left small comments.
It's almost ready for my final review/edits 🥇
@ChihSeanHsu Do you have time to keep working on this? |
@felixxm |
637bd4f
to
54244ac
Compare
I squashed commits and rebased from the |
5f2e849
to
c32e98f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChihSeanHsu I pushed edits to docs and main changes. I'm going to review the tests tomorrow.
f055eaa
to
9aeee21
Compare
@ChihSeanHsu Thanks again for all your efforts 🥇 Welcome aboard ⛵ I pushed edits to tests. IMO, it's ready 🥳 |
9aeee21
to
fba23da
Compare
fba23da
to
0e62ffe
Compare
…_create(). Thanks Florian Apolloner, Chris Jerdonek, Hannes Ljungberg, Nick Pope, and Mariusz Felisiak for reviews.
0e62ffe
to
0f69464
Compare
@felixxm |
Ticket #31685
Adding
upsert_conflicts
arguments tobulk_create
.This change only supports to mysql, sqlite3 and postgresql.