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

Feature/issue59 #60

Open
wants to merge 2 commits into
base: feature/customUserModel
Choose a base branch
from
Open

Feature/issue59 #60

wants to merge 2 commits into from

Conversation

msolomon
Copy link
Collaborator

Should fix #59

I don't think this actually changes the logic, since we never check any of the locked rows bar the latest. @hoddez should verify

def bulk_create_with_manual_ids(self,instances):
try:
start = self.select_for_update().all().order_by('-pk')[0].pk + 1
except IndexError:
start = self.select_for_update().latest(field_name='pk').pk + 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool that's much cleaner. I'm guessing it does the same thing under the hood tho right?
(fyi the [0] causes a LIMIT 1 on the query so it doesnt get all pks)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also not sure if it's causing any trouble but I think the select_for_update can be removed. I originally put it in to try and fix the race condition but that never really worked out as you know..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checked, its the same thing. wont make anything faster but is nicer looking:
In [9]: User.objects.all().values('id').order_by('-pk')[0]
DEBUG util (0.000) SELECT auth_user.id FROM auth_user ORDER BY auth_user.id DESC LIMIT 1; args=()

In [10]: User.objects.values('id').latest('pk')
DEBUG util (0.000) SELECT auth_user.id FROM auth_user ORDER BY auth_user.id DESC LIMIT 1; args=()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

SELECT `sendgrid_­emailmessage``id``sendgrid_­emailmessage``message_­id``sendgrid_­emailmessage``from_­email``sendgrid_­emailmessage``to_­email``sendgrid_­emailmessage``category``sendgrid_­emailmessage``response``sendgrid_­emailmessage``creation_­time``sendgrid_­emailmessage``last_­modified_­time` FROM `sendgrid_­emailmessage` ORDER BY `sendgrid_­emailmessage``id` DESC LIMIT ? FOR UPDATE

I think the solution is something like

self.select_for_update().values('pk').latest(field_name='pk')[0]["pk"] + 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? latest returns an instance of the model, not a list or queryset so that would raise an exception. Also are we sure that query you posted is coming from this line?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If latest doesn't just slice then maybe that's not quite right. The point it that it only selects values which are indexed.

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.

3 participants