Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#9231 closed (invalid)

get_or_create creates even if the object is already there

Reported by: canburak Owned by: nobody
Component: Database layer (models, ORM) Version: 0.96
Severity: Keywords:
Cc: vst@…, tolga.tatari@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

my model is

class InvitationStatistic(models.Model):
    email_address = models.EmailField(max_length=125,
                                      db_index=True,
                                      unique=True)

    number_of_retries = models.IntegerField(default=0)

the code triggered the error is?

obj, created = InvitationStatistic.objects.get_or_create(email_address=self.invitee)

error is:

2008-09-28 08:17:56 EEST ERROR:  duplicate key value violates unique constraint "user_invitationstatistic_email_address_key"
2008-09-28 08:17:56 EEST STATEMENT:  INSERT INTO "user_invitationstatistic" ("email_address", "number_of_retries") VALUES (E'user@example.com', 0)

Change History (4)

comment:1 Changed 7 years ago by canburak

  • Cc vst@… tolga.tatari@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by badri

  • Resolution set to invalid
  • Status changed from new to closed
  • Triage Stage changed from Unreviewed to Accepted

This might be similar to #9269.

obj, created = InvitationStatistic.objects.get_or_create(email_address=self.invitee)

will try to create an entry with email=self.invitee and number_of_retries = 0. if an entry is probably present with key=self.invitee, you'll get a unique constraint violation.

comment:3 Changed 7 years ago by mtredinnick

Since this was reported against 0.96, it may be kind of a variation on #9269. We've made a number of changes to solidify get_or_create() in the period between 0.96 and 1.0, including catching race conditions for creation.

If the original reporter is seeing the same problem with Django 1.0, please include details of the database backend used and try to capture the traceback at the point it is raised inside get_or_create(). We should be correctly handling all integrity errors there now.

comment:4 Changed 7 years ago by mtredinnick

(By the way, this would have been better closed as "fixed", rather than "invalid", since it was certainly a possible error in 0.96.)

Note: See TracTickets for help on using tickets.
Back to Top