P
P
Pan Propan2016-03-04 00:02:13
Django
Pan Propan, 2016-03-04 00:02:13

How to fix django.db.utils.IntegrityError: UNIQUE constraint failed error?

Created a model

from datetime import date, timedelta, datetime
from django.db import models
from shortuuidfield import ShortUUIDField

class Customer(models.Model):
    uuid = ShortUUIDField(unique=True)
    firstname = models.CharField(max_length=100, verbose_name="Имя", blank=False, null=True)
    lastname = models.CharField(max_length=300, verbose_name="Фамилия", blank=False, null=True)
    upload_path = 'patient/'
    photo = models.ImageField(upload_to=upload_path, verbose_name="Фото профиля", null=True)
    MAN = 'M'
    FEMALE = 'F'
    GENDER_CHOICES = (
        (MAN, 'Мужской'),
        (FEMALE, 'Женский'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=MAN, verbose_name="Пол",)
    midle_name = models.CharField(max_length=300, verbose_name="Отчетство", blank=True, null=True)
    birthday = models.DateField(verbose_name="Дата рождения")
    phone = models.CharField(max_length=20, verbose_name="Номер телефона")
    email = models.EmailField(max_length=200, verbose_name="Электронный адрес")

    class Meta:
        verbose_name = 'Пациент'
        verbose_name_plural = 'Пациенты'

    def __str__(self):
        return "%s %s" % (self.firstname, self.lastname)

I'm trying to migrate and I'm
migrate patient
getting an error
django.db.utils.IntegrityError: UNIQUE constraint failed: patient_customer.uuid

SQLITE database, UUID extension also installed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-03-04
@deliro

It's obvious. You set the unique=True flag, but there are already non-unique uuids in the database.
PS Why do you need a UUID if it's not a Primary Key?
PSS Why do we need a Short UUID at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question