Opened 5 months ago

Last modified 5 months ago

#35551 closed Bug

Unique checks skipped when changing primary key — at Version 6

Reported by: Csirmaz Bendegúz Owned by: Csirmaz Bendegúz
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Csirmaz Bendegúz)

I noticed the unique checks are skipped (_perform_unique_checks) when an object's primary key is changed.

Given:

# models.py
class Dummy(models.Model):
    id = models.IntegerField(primary_key=True)

# admin.py
admin.site.register(Dummy)
  1. Create Dummy (1), Dummy (2).
  2. Change Dummy (2)'s Id field -> 1
  3. No error, unique checks skipped
  4. Change Dummy (1)'s Id field -> 3
  5. A new Dummy (3) is created, Dummy (1) stays unchanged

I believe step 5 should never happen, since step 3 should fail with a unique check error.

Change History (6)

comment:1 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)

comment:2 by Sarah Boyce, 5 months ago

Triage Stage: UnreviewedAccepted

Great catch thank you!

I believe step 5 should never happen, since step 3 should fail with a unique check error.

Agreed 👍 if you were to add another field to the model then you slightly override Dummy (1) with whatever is in Dummy (2) - which is not nice.

comment:3 by Csirmaz Bendegúz, 5 months ago

Owner: changed from nobody to Csirmaz Bendegúz
Status: newassigned

comment:4 by Csirmaz Bendegúz, 5 months ago

So actually there are 2 issues:

  1. The unique check is skipped when PK is changed.
  2. The object is duplicated when PK is changed.

I'm looking into this.

comment:5 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)

comment:6 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top