Opened 2 years ago

Last modified 2 years ago

#33912 closed Bug

In the admin, saving an object in a non-default database with unique field fails when the default database contains an object with the same unique field value but different PK — at Initial Version

Reported by: François Granade Owned by: nobody
Component: contrib.admin Version: 4.0
Severity: Normal Keywords: admin multi-db
Cc: François Granade Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Reproducible using the test project in https://github.com/farialima/django-multidbbug . See README.md on how to run it.

The steps are:

  • a model with a unique string field (beside the default int PK)
  • two DBs in the settings (default and other)
  • two objects in the default DB with PK 1 and 2, and some unique value for the unique string field
  • a object in the other DB (thus, with PK 1) with a different unique string field value
  • in admin.py, point the UI to the other database for the model (using https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface)
  • in the admin, try modifying the object in the other DB, giving it the same unique string value as the object with PK 2 in the default database

=> saving fails with error "Parcel with this Tracking id already exists.", see screenshot.

The underlying issue is clearly that the admin will try to verify in the default DB that the objects doesn't exist. I think that the admin should *not* look in the default DB, as I set it up to only use the 'other' DB for my model...

`

% poetry run ./manage.py shell

(...)

from multidbbugapp.models import Parcel
print(Parcel.objects.all())

<QuerySet [<Parcel: id=1, tracking_id=one>, <Parcel: id=2, tracking_id=two>]>

print(Parcel.objects.using('other').all())

<QuerySet [<Parcel: id=1, tracking_id=two-duplicate>]>

o = Parcel.objects.using('other').first()
o

<Parcel: id=1, tracking_id=two-duplicate>

o.tracking_id = 'two'
o.save()

`

Change History (1)

by François Granade, 2 years ago

screenshot of the error

Note: See TracTickets for help on using tickets.
Back to Top