0

My model:

class DirectoryDoctors (models.Model):
    num = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    design_choices = (
        ('IMO', 'IMO'),
        ('AIMO', 'AIMO'),
    )
    designation = models.CharField(
        choices=design_choices, max_length=30, default='unspecified')
    mobile = models.CharField(max_length=15, default='')
    alternate = models.CharField(max_length=15, default='', blank=True)
    email = models.CharField(max_length=50, default='', blank=True)
    dob = models.DateField(null=True, blank=True)
    specialiast_or_not_choices = (
        ('Yes', 'Yes'),
        ('No', 'No'),
        ('Unspecified', 'Unspecified')
    )
    specialiast = models.CharField(
        choices=specialiast_or_not_choices, max_length=30, default='Unspecified')
    specialty_choices = (
        ('Internal Medicine', 'Internal Medicine'),
        ('General Surgery', 'General Surgery'),
        ('Not Applicable', 'Not Applicable')
    )
    specialty = models.CharField(
        choices=specialty_choices, max_length=30, default='Unspecified')
    institution = models.ForeignKey(DirectoryHospital, on_delete=models.DO_NOTHING)
    bloodgroup_choices = (('apos', 'A+'),
        ('-', '-')
        )
    bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=15, default='-', blank=True)

    spousename = models.CharField(max_length=100, blank=True)
    children = models.CharField(max_length=200, blank=True)
    present_address = models.CharField(max_length=200, blank=True)
    permanent_address = models.CharField(max_length=200, blank=True)


    class Meta:
        unique_together = ["name", "mobile", "email"]

    def __str__(self):
        st = f"{self.name}, {self.designation}, {self.institution}"
        return st

And code while updating data:

dirhosp = DirectoryHospital.objects.get(name = disp, insttype = hospordisp, district = district)

try:
    dirdoc = DirectoryDoctors.objects.update_or_create(name = name, designation = desig, mobile = mob, alternate = alt, email = emailadd, dob = dob, specialiast = specialist_not, specialty = specialty, institution = dirhosp, bloodgroup = blgp, spousename = spouse, children = children, present_address = address_pres, permanent_address = address_perm)

    print(f"Saved name = {name}, designation = {desig}, mobile = {mob}, alternate = {alt}, email = {emailadd}, dob = {dob}, specialiast = {specialist_not}, specialty = {specialty}, institution = {dirhosp}, bloodgroup = {blgp}, spousename = {spouse}, children = {children}, present_address = {address_pres}, permanent_address = {address_perm}\n")
except Exception as e:                
    if "NaTType does not support utcoffset" in e.args[0]:
        dirdoc = DirectoryDoctors.objects.update_or_create(name = name, designation = desig, mobile = mob, alternate = alt, email = emailadd, specialiast = specialist_not, specialty = specialty, institution = dirhosp, bloodgroup = blgp, spousename = spouse, children = children, present_address = address_pres, permanent_address = address_perm)
        print("No proper DOB")
        print(f"Saved by alt: name = {name}, designation = {desig}, mobile = {mob}, alternate = {alt}, email = {emailadd}, specialiast = {specialist_not}, specialty = {specialty}, institution = {dirhosp}, bloodgroup = {blgp}, spousename = {spouse}, children = {children}, present_address = {address_pres}, permanent_address = {address_perm}\n")
    else:
        print("Another uncaught exception occured!")
        print(e.args[0], '\n')

The problem:

Name: A Station:Peroorkada Type:Hospital specialist:Yes specialty:Ophthalmology
Saved name = A, designation = AIMO, mobile = 0, alternate = , email = a@gmail.com, dob = 1999-05-30 00:00:00, specialiast = Yes, specialty = Ophthalmology, institution = ESI Hospital Peroorkada, Trivandrum, bloodgroup = O+, spousename = Dr , children = ddd, present_address = medical college p.o 695011, permanent_address = 

Name: B Station:Ernakulam Type:Hospital specialist:Yes specialty:Anaesthesia
Another uncaught exception occured!
duplicate key value violates unique constraint "app_directorydoctors_name_mobile_email_71312dd8_uniq"
DETAIL:  Key (name, mobile, email)=(B, 1234, a.nb@gmail.com) already exists.

Why does this happen? How can I fix this?

A similiar question has been asked here, but in that, apparently the problem is due to use of defaults parameter in update_or_create. I feel that this is not applicable in my case, though the end result is the same error

Joel G Mathew
  • 7,561
  • 15
  • 54
  • 86
  • You have defined `unique_together = ["name", "mobile", "email"]`which means that your `update_or_create` should take this into consideration. You are ignoring this constraint by adding additional arguments to your `update_or_create`. That similar question you're linking to is exactly the same issue. – Borut Jan 15 '19 at 20:20
  • So I should not be updating name, email, mobile when I use update_or_create? – Joel G Mathew Jan 15 '19 at 20:41
  • No, check the accepted answer in the linked question. – Borut Jan 15 '19 at 20:52

0 Answers0