﻿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
20301	Unique field validation with multiple DB connections.	SardarNL	nobody	"`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()`."	Bug	closed	Database layer (models, ORM)	dev	Normal	duplicate			Accepted	0	0	0	0	0	0
