[Django] #28253: Integrity error in get_or_create

17 views
Skip to first unread message

Django

unread,
May 29, 2017, 3:07:36 PM5/29/17
to django-...@googlegroups.com
#28253: Integrity error in get_or_create
-------------------------------------+-------------------------------------
Reporter: Anuranjit | Owner: Anuranjit maindola
maindola |
Type: Bug | Status: assigned
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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:
}}}

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

Django

unread,
May 29, 2017, 4:35:15 PM5/29/17
to django-...@googlegroups.com
#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

unread,
May 30, 2017, 4:37:24 AM5/30/17
to django-...@googlegroups.com
#28253: Integrity error in get_or_create
-------------------------------------+-------------------------------------
Reporter: Anuranjit maindola | Owner: 冯钊
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
-------------------------------------+-------------------------------------
Changes (by 冯钊):

* owner: Anuranjit maindola => 冯钊


--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:2>

Django

unread,
May 30, 2017, 11:04:11 AM5/30/17
to django-...@googlegroups.com
#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
-------------------------------------+-------------------------------------
Changes (by Anuranjit maindola):

* owner: 冯钊 => Anuranjit maindola


--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:3>

Django

unread,
May 31, 2017, 7:37:23 AM5/31/17
to django-...@googlegroups.com
#28253: Integrity error in get_or_create
-------------------------------------+-------------------------------------
Reporter: Anuranjit maindola | Owner: Anuranjit
| maindola
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: assigned => closed
* resolution: => invalid


Comment:

Absent a justification for the double save, this looks like invalid usage
rather than a bug in Django.

--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:2>

Django

unread,
Aug 28, 2017, 6:06:07 AM8/28/17
to django-...@googlegroups.com
#28253: Integrity error in get_or_create
-------------------------------------+-------------------------------------
Reporter: Anuranjit maindola | Owner: Anuranjit
| maindola
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Anuranjit maindola):

@Tim Graham .
Doing any number of saves should be fine.
Can you elaborate on why is this is not a valid issue?

--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:3>

Django

unread,
Aug 28, 2017, 6:20:25 AM8/28/17
to django-...@googlegroups.com
#28253: Integrity error in get_or_create
-------------------------------------+-------------------------------------
Reporter: Anuranjit maindola | Owner: Anuranjit
| maindola
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Marten Kenbeek):

The issue is that you save twice with `force_insert=True`, without
changing the value of the primary key. If you want to update the object
after the first save, you need to strip the `force_insert` argument from
`**kwargs`.

--
Ticket URL: <https://code.djangoproject.com/ticket/28253#comment:4>

Reply all
Reply to author
Forward
0 new messages