• 15

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

model example

class Example(Stat):
    numeric = models.IntegerField(...) 
    date = models.DateField( auto_now_add=True,...) #auto_now_add=True was the problem

    class Meta:
       unique_together = ('numeric','date')


If 72 and '2011-08-07' is already stored



django.db.utils.IntegrityError: (1062, "Duplicate entry '72-2011-08-07'

the question is why get_or_create raises the IntegrityError, thats the idea of using get_or_create.

Not sure if this is a bug, I opened a ticket https://code.djangoproject.com/ticket/16587

It appears your problem is with there being more columns you're not including in your get_or_create, see i.e. this thread on a Django mailing list.

You need to use the defaults parameter of get_or_create as described in the docs, or specify values for all columns, for get_or_create to match correctly.

  • 24
Reply Report
      • 2
    • I understand that type of use I would need defaults,but my params in get_or_create are the entire fields avaidable in the class. Anyways that information helped a lot. Also if I remove the unique_together the get_or_create works.
    • get_or_create is also partially case sensitive -- if they have different case, it won't find the match, but you might still hit the uniqueness constraint.
    • I solved the problem reading your answer. DateField was using auto_now_add=True , so I avoided some important information in my question. To solve auto_now_add=False is working now
      • 2
    • @llazzaro Why can't 'DateField was using auto_now_add=True' be added to your defaults?

Trending Tags