#31286: Database specific checks always goes to the 'default' backend
-------------------------------------+-------------------------------------
               Reporter:  Hongtao    |          Owner:  nobody
  Ma                                 |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  master
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 In an attempt to trigger the check Error mentioned in Tickets: #31144, I
 found that the check for database backend doesn't check against the
 backend specified, e.g. --database mysql, rather, it always checks against
 the 'default' backend.

 After diving into the source code, I found the following method defined in
 django/db/models/fields/__init__.py
 {{{
     def _check_backend_specific_checks(self, **kwargs):
         app_label = self.model._meta.app_label
         for db in connections:
             if router.allow_migrate(db, app_label,
 model_name=self.model._meta.model_name):
                 return connections[db].validation.check_field(self,
 **kwargs)
         return []
 }}}
 It checks the first db defined in connections rather those provided by
 users.

 A proposed change would be:
 {{{
     def _check_backend_specific_checks(self, **kwargs):
         app_label = self.model._meta.app_label
         errors = []
         for db in kwargs['databases'] or ['default']:
             if router.allow_migrate(db, app_label,
 model_name=self.model._meta.model_name):
                 errors.extend(connections[db].validation.check_field(self,
 **kwargs))
         return errors
 }}}

 It worked as intended on my local machine.
 I would happily provide a patch for this one.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31286>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.dba89b3e04cfa65489de039778881fb3%40djangoproject.com.

Reply via email to