[Django] #20301: Unique field validation with multiple DB connections.

4 views
Skip to first unread message

Django

unread,
Apr 22, 2013, 10:14:15 AM4/22/13
to django-...@googlegroups.com
#20301: Unique field validation with multiple DB connections.
----------------------------------------------+--------------------
Reporter: SardarNL | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
`django.db.models.base._perform_unique_checks` is not using
`self._state.db` to lookup unique fields. Check the following:

{{{
class MyModel(models.Model):
my_unique = models.IntegerField(unique=True)

obj = MyModel(my_unique=value_exists_on_default_new_on_other_connection)
obj._state.db = other_connection
obj.full_clean()

# for existing objects _state.db will be already set
obj = MyModel.objects.using(other_connection).get(pk=existing)
obj.my_unique = value_exists_on_default_new_on_other_connection
obj.full_clean()
}}}

`full_clean()` will `validate_unique()` -> `_perform_unique_checks()`,
which will eventually `qs =
model_class._default_manager.filter(**lookup_kwargs)`. The default manager
is always using default connection, so unique check will be performed on
default connection. Other validators, such as ForeignKey lookup, properly
use `.using()` to keep talking to the database where the object came from.

Fix: {{{ qs =
model_class._default_manager.filter(**lookup_kwargs).using(self._state.db)
}}}
The same fix is needed for `_perform_date_checks()`.

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

Django

unread,
Apr 22, 2013, 5:51:46 PM4/22/13
to django-...@googlegroups.com
#20301: Unique field validation with multiple DB connections.
-------------------------------------+-------------------------------------

Reporter: SardarNL | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jacob):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/20301#comment:1>

Django

unread,
Jul 29, 2015, 9:26:01 AM7/29/15
to django-...@googlegroups.com
#20301: Unique field validation with multiple DB connections.
-------------------------------------+-------------------------------------
Reporter: SardarNL | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #15130

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

Reply all
Reply to author
Forward
0 new messages