Wednesday, November 23, 2022

Re: getting unique id

Why not use the existing Django AutoField?

- Tom

> On Nov 23, 2022, at 8:56 AM, Larry Martell <larry.martell@gmail.com> wrote:
>
> I have an app that needs to get a unique ID. Many threads run at the
> same time that need one. I would like the IDs to be sequential. When I
> need a unique ID I do this:
>
> with transaction.atomic():
> max_batch_id =
> JobStatus.objects.select_for_update(nowait=False).aggregate(Max('batch_id'))
> json_dict['batch_id'] = max_batch_id['batch_id__max'] + 1
> status_row = JobStatus(**json_dict)
> status_row.save()
>
> But multiple jobs are getting the same ID. Why does the code not work
> as I expect? What is a better way to accomplish what I need?
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACwCsY54vF6ZK7yy--15hy1u5vxJ%2Bq9Zp_ODGfAX973gPYpecw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/59505632-CCFA-4356-86B8-B264528E6A69%40gmail.com.

No comments:

Post a Comment