#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to