2021年3月6日星期六

IntegrityError :UNIQUE constraint failed: theatre_audience.ticketId

I am trying to create a Django application to assign seats for the audience in a theatre(for live performances). Whenever I try to add a new audience, I cannot add more than one audience. It gives UNIQUE constraint failed error. Here, in my code, I am trying to create a ticketId using the UUID field. But, it is not getting saved for more than one audience.

my model

from django.db import models  import uuid    # Create your models here.  class Audience(models.Model):      name = models.CharField(max_length=200, null=True)      ticketId = models.UUIDField(              primary_key = True,              default = uuid.uuid4(),              editable = False          )      seatNo = models.IntegerField(default=0)  

my application

def occupy(request, pname):      arr = [i for i in range(1,Total_seats+1)]      inst = Audience()      inst.name = pname      num = Audience.objects.values_list('seatNo')      print(num)      for i in num:          v = i[0]          arr.remove(i[0])      print(arr)      if(len(arr)<1):          msg="All seats are occupied"          context={"msg":msg}          return render(request,"theatre/ERROR.html",context)      else:          seatn = random.choice(arr)          inst.seatNo = seatn          inst.save()          temp = Audience.objects.get(seatNo = seatn)          context = {"temp":temp}          return render(request, "theatre/show.html", context)  

I am getting error in the following line The above exception (UNIQUE constraint failed: theatre_audience.ticketId) was the direct cause of the following exception:

    inst.save()   
https://stackoverflow.com/questions/66513093/integrityerror-unique-constraint-failed-theatre-audience-ticketid March 07, 2021 at 12:07PM

没有评论:

发表评论