#28253: Integrity error in  get_or_create
-------------------------------------+-------------------------------------
     Reporter:  Anuranjit maindola   |                    Owner:  Anuranjit
                                     |  maindola
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Old description:

> I came across an issue. Consider a code example below
> {{{
> class Config(models.Model):
>     key = models.CharField(max_length=100)
>     value = models.CharField(max_length=100)
>     def save(self, *args, **kwargs):
>          super(Config, self).save(*args, **kwargs)
>          super(Config, self).save(*args, **kwargs)
> }}}
> Now if for the Above model I do
>          {{{
>          params = {"key":"1", "value": "2")
>          obj, created = Config.objects.get_or_create(**params)
> }}}
> I get an Integrity Error for Primary Key i.e. the database `id` field.
>
> The issue as i understood comes because Django tries to insert again with
> duplicate Primary key. In `get_or_create` the `create` method is called
> with `force_insert=True` and a particular check in `_save_table` fails
> (line number 897) . (If this check passes django is supposed to do an
> update).
>
> {{{
>     # If possible, try an UPDATE. If that doesn't update anything, do an
> INSERT.
>     if pk_set and not force_insert:
> }}}

New description:

 I came across an issue. Consider a code example below
 {{{
 class Config(models.Model):
     key = models.CharField(max_length=100)
     value = models.CharField(max_length=100)
     def save(self, *args, **kwargs):
          super(Config, self).save(*args, **kwargs)
          super(Config, self).save(*args, **kwargs)
 }}}
 Now if for the Above model I do
          {{{
          params = {"key":"1", "value": "2"}
          obj, created = Config.objects.get_or_create(**params)
 }}}
 I get an Integrity Error for Primary Key i.e. the database `id` field.

 The issue as i understood comes because Django tries to insert again with
 duplicate Primary key. In `get_or_create` the `create` method is called
 with `force_insert=True` and a particular check in `_save_table` fails
 (line number 897) . (If this check passes django is supposed to do an
 update).

 {{{
     # If possible, try an UPDATE. If that doesn't update anything, do an
 INSERT.
     if pk_set and not force_insert:
 }}}

--

Comment (by Tim Graham):

 What's the reason for calling `super().save()` twice?

--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.0a9f962a83cdc98e4341b6124af5c796%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to