0

I am creating a site that will be connected to 4 databases. I got the first three going no problem. I created a separate app for each db then created a router for each one. The problem with the last db is that the router is not triggering. It keeps sending the traffic to the default db. The default db is app2, and the db I want to use is 'Login'. Here is my router

 class LoginRouter(object):
     def db_for_read(self, model):

         if model._meta.app_label == 'Login':
             return 'Login'
         return 'default'

Here is my Settings declaration:

 DATABASE_ROUTERS = ['reports.dbrout.CucRouter', 'reports.dbrout.CpsgRouter', 'reports.dbrout.LoginRouter', ]

Here is the model in the 'Login' app that is not connecting to the right db.

 from __future__ import unicode_literals

 from django.db import models


 class TblUsers(models.Model):
     userid = models.AutoField(db_column='userID', primary_key=True)
     username = models.CharField(db_column='userName', max_length=100)  
     useremail = models.CharField(db_column='userEmail', unique=True, max_length=100) 
     userpass = models.CharField(db_column='userPass', max_length=100) 
     userstatus = models.CharField(db_column='userStatus', max_length=1)  
     tokencode = models.CharField(db_column='tokenCode', max_length=100)  
     companyid = models.CharField(db_column='companyID', max_length=255, blank=True, null=True)
     fk_customer = models.IntegerField(blank=True, null=True)
     is_admin = models.IntegerField(blank=True, null=True)

     class Meta:
         managed = False
         db_table = 'tbl_users'
         app_label = 'Login'

     def __str__(self):
         return str(self.userid)

Full dbrout page:

 class CucRouter(object):
     def db_for_read(self, model):

         if model._meta.app_label == 'CUCMCDR':
             return 'CUCMCDR'
         return 'default'


  class CpsgRouter(object):
      def db_for_read(self, model):

         if model._meta.app_label == 'CPSG':
             return 'CUCMCDR'
         return 'default'


 class LoginRouter(object):
     def db_for_read(self, model):

         if model._meta.app_label == 'Login':
             return 'Login'
         return 'default'

Like I said this worked for the first three. What am I missing here that is not catching for the last one!!???

Joe
  • 2,641
  • 5
  • 22
  • 43
  • I think we need more information. Can you include the declaration of the model that you are passing to the db_for_read method? – Jeremy Klein May 17 '17 at 13:45
  • Yes Ill edit that in right now – Joe May 17 '17 at 13:50
  • sequence of DATABASE_ROUTERS matters, so add reports.dbrout.LoginRouter at first place then try. may be it will work. – Sanjay May 17 '17 at 14:00
  • @Sanjay Hey THAT WORKED! BUT... it now broke the others. What is the correct way to list these so that they will not do this???? – Joe May 17 '17 at 14:02
  • @Joe Can you provide the `DATABASES` settings? – John Moutafis May 17 '17 at 14:10
  • @JohnMoutafis The settings are correct. The problem is definitely where Sanjay mentioned. It is a problem with the order. The new problem is no matter how I rearrange it I end up breaking one of them – Joe May 17 '17 at 14:15

1 Answers1

0

The order in which routers are processed is significant. Routers will be queried in the order they are listed in the DATABASE_ROUTERS setting.
Add it like below way.
DATABASE_ROUTERS = ['reports.dbrout.LoginRouter','reports.dbrout.CucRouter', 'reports.dbrout.CpsgRouter',]

While setting it make sure your order, if Login app_label is used in LoginRouter and CucRouter then LoginRouter will process before CucRouter

Sanjay
  • 1,958
  • 2
  • 17
  • 25
  • I have it set like that now and it makes the Login app work but it will break the two other routers. I only shifted the problem. I have each router in its own class. Maybe I can if/else chain the routes in one class?? – Joe May 17 '17 at 14:14
  • @Joe, can you add your all router classes in question? So it will give clear Idea. – Sanjay May 17 '17 at 14:21
  • Yes adding those in now. – Joe May 17 '17 at 14:24
  • now add return None in LoginRoute instead of return 'default' and then try. – Sanjay May 17 '17 at 14:34
  • You my friend are a Django hero. That is exactly what it was. THANKYOU. I will mark your answer as correct!!!! – Joe May 17 '17 at 14:38
  • I am glad to hear that.. Thanks Joe – Sanjay May 17 '17 at 14:49