Opened 10 years ago

Closed 10 years ago

Last modified 10 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: no UI/UX: no


my model is

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

    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'', 0)

Change History (4)

comment:1 Changed 10 years ago by canburak

Cc: vst@… tolga.tatari@… added

comment:2 Changed 10 years ago by Badri

Resolution: invalid
Status: newclosed
Triage Stage: UnreviewedAccepted

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 10 years ago by Malcolm Tredinnick

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 10 years ago by Malcolm Tredinnick

(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