Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

python - django.db.utils.IntegrityError: UNIQUE constraint failed: new__Boutique_boutique.product_id

I had 7 unapplied migrations when I ran manage.py migrate I got a unique constraint error

Models.py :

from django.db import models
import uuid

class boutique(models.Model):
  product_id = models.AutoField
  desc = models.CharField(max_length=300)
  pub_date = models.DateTimeField(auto_now_add=True)
  product_name = models.CharField(max_length=50)
  category = models.CharField(max_length=50, default="")
  price = models.IntegerField(default=0)
  image = models.ImageField(upload_to='index/images', default="")
  slug = models.SlugField(unique=True, default=uuid.uuid1)

  def __str__(self):
     return self.product_name

I have added the slug field according to a similar question on stack but that doesn't work. Views.py : `

from django.shortcuts import render
from math import ceil
from .models import boutique
from django.http import HttpResponse


def Boutique(request):
  allProds = []
  catprods = boutique.objects.values('category', 'id')
  cats = {item['category'] for item in catprods}
  for cat in cats:
    prod = boutique.objects.filter(category=cat)
    n = len(prod)
    nSlides = n // 4 + ceil((n / 4) - (n // 4))
    allProds.append([prod, range(1, nSlides), nSlides])

  # params = {'no_of_slides':nSlides, 'range': range(1,nSlides),'product': products}
  # allProds = [[products, range(1, nSlides), nSlides],
  #             [products, range(1, nSlides), nSlides]]
  params = {'allProds': allProds}
  return render(request, 'Boutique/Boutique.html', params)

def individual(request,product_id): #fetching product using the id

   all_products = boutique.objects.filter(id = product_id)
   context = {'all_products' : all_products[0]}

   return render(request,'Boutique/individual.html' , context )
question from:https://stackoverflow.com/questions/65650471/django-db-utils-integrityerror-unique-constraint-failed-new-boutique-boutique

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...