﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28253	Integrity error in  get_or_create	Anuranjit maindola	Anuranjit maindola	"
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:
}}}

"	Bug	closed	Database layer (models, ORM)	1.11	Normal	invalid			Unreviewed	0	0	0	0	0	0
