Skip to content
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

Bulk update or create #66

Closed
wants to merge 7 commits into from
Closed

Bulk update or create #66

wants to merge 7 commits into from

Conversation

jonathan-s
Copy link

@jonathan-s jonathan-s commented Sep 1, 2017

This should fix #49.

There is probably still improvements to be made to this PR but the initial approach seems to work for me. So I'm submitting this for review.

This PR basically adds the functionality of using update_or_create to the BulkUpdateManager. Which has been requested for some time.

The approach I took to accomplish this is to first separate all objects that have an id in a batch. Then make a select statement to check that they actually exist in the database. I update all of these objects using the already existing bulk_update method.

The leftover objects didn't exist in the database, thus they need to be inserted. Since technically you can create an object and still get a collision the insertion of those objects take place with a custom crafted upsert.

So for instance if you have a field that is unique it will avoid a collision with that uniqueness and instead do an update instead of an insertion.

@jonathan-s
Copy link
Author

Making a note that travis is actually only testing postgres at the moment because of this change

@jonathan-s
Copy link
Author

@aykut and @arnau126 Might want to review this

@tony
Copy link

tony commented Oct 18, 2017

This may be worth looking at: http://django-postgres-extra.readthedocs.io/manager/ (Postgres only)

@jonathan-s jonathan-s closed this Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bulk_update_or_create(model_instances) or bulk_update(model_instances, upsert=True)?
2 participants