Monday, November 28, 2022

Re: getting unique id

On Wed, Nov 23, 2022 at 3:11 PM Larry Martell <larry.martell@gmail.com> wrote:
>
> On Wed, Nov 23, 2022 at 3:01 PM Thomas Lockhart <tlockhart1976@gmail.com> wrote:
> >
> > Why not use the existing Django AutoField?
>
> Because I have multiple rows with the same batch_id, and also I would
> like the batch_ids to be sequential.
>
> The use case is a batch job dashboard. Users run jobs that spawn many
> sub jobs. The jobs all record their status in the JobStatus table. The
> batch dashboard shows all the sub jobs grouped under their parent job.
> The parent job creates a row with a new batch_id, and that is passed
> to the sub jobs. They all record their status using that same batch
> id, but each in its own row.
>
> > > 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?

I ended up using another table to get the batch_ids from. It works,
but I still wonder how to make it work without a second table.

--
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/CACwCsY57mQ%2Bp19gPf21AuhwXi66yatSY10gdSWuGk9s5XZx-HQ%40mail.gmail.com.

No comments:

Post a Comment