Search code examples
djangopostgresqldjango-modelssharding

How to add another database server node in Django


I've a 2 databases which I've been running on a single machine. my question is how how can I add More database server along with that I wanna use django sharding so how can I shard my data from both database according to users means user 1 data from db1 and db2 on 1st server user 2's data on another


Solution

  • You need to define a router in settings:

    DATABASE_ROUTERS = ['routers.routers.ModelDatabaseRouter']
    

    in routers create file routers:

    class ModelDatabaseRouter(object):
        """Allows each model to set its own destiny"""
    
        def db_for_read(self, model, **hints):
            # Specify target database with field in_db in model's Meta class
            if hasattr(model._meta, 'in_db'):
                return model._meta.in_db
            return None
    
        def db_for_write(self, model, **hints):
            # Specify target database with field in_db in model's Meta class
            if hasattr(model._meta, 'in_db'):
                return model._meta.in_db        
            return None
    
        def allow_syncdb(self, db, model):      
            # Specify target database with field in_db in model's Meta class
            if hasattr(model._meta, 'in_db'):
                if model._meta.in_db == db:
                    return True
                else:
                    return False
            else:
                # Random models that don't specify a database can only go to 'default'
                if db == 'default':
                    return True
                else:
                    return False
    

    (from ) https://djangosnippets.org/snippets/2687/

    Now you can define the DB in your models like this:

    class YourClass(models.Model):
        name = models.CharField(primary_key=True, max_length=50)
        creation_time = models.DateTimeField()
    
        class Meta:
            in_db = 'api'  <-- THIS
    

    The db must be defined in settings:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'XXXXX',
            'USER': 'XXXXX',
            'PASSWORD': 'XXXXXX',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        },
        'api': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'xxxx',
            'USER': 'xxxx',
            'PASSWORD': 'xxxxxx',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        }
    }