﻿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
31321	Unexpected behavior of `update_or_create`, misleading flag `created`.	plushchaynikolay	nobody	"Unexpected behavior of `update_or_create` (and `get_or_create`) when accidentally overrode primary_key with None-value.
It was primarily unexpected because of misleading naming of flag `created`. 

{{{
    . . .
    self.assertEqual(MyModel.objects.count(), 1)
    # ok

    _, created = MyModel.objects.update_or_create(
        somefield=obj.somefield,
        defaults={**model_to_dict(obj)}
    )

    self.assertFalse(created)
    # ok

    self.assertEqual(MyModel.objects.count(), 1)
    # AssertionError: 2 != 1
}}}

`created == False` but `MyModel.objects.count() == 2`, and new object was actually created.

{{{
>>> model_to_dict(obj)
{'id': None, 'somefield': 'once told me'}
}}}

The field `id` (primary key) is overridden with `None` from `defaults={**model_to_dict(obj)}`, and when it does`obj.save()`, then `obj.id` is auto incremented with new value. (Same with `get_or_create`)

We understand that the issue took it's place because of our unknowing of how this functions work inside. But actually we shouldn't know it.

Also have some suggestions, if You don't mind:
    - rename `created` into `found`
    - carefully explain this issue in documentation
    - check if field is autoincrementable and overridden to a None-value and ignore overriding
    - check if field is autoincrementable and overridden to a None-value and make a warning

[https://github.com/plushchaynikolay/dj-bug more], desu"	Cleanup/optimization	closed	Database layer (models, ORM)	3.0	Normal	wontfix	get_or_create update_or_create created primary_key		Unreviewed	0	0	0	0	1	0
