0

I am trying to setup celery in one of my django projects. I want celery to use a separate database. Currently, as the project is in development phase we are using sqlite3. In order to setup multiple databases i did the following.

Defined databases in the settings.py file.

DATABASES = {'default':
                {'ENGINE': 'django.db.backends.sqlite3',
                 'NAME':'devel',
                 'USER':'',
                 'PASSWORD':'',
                 'HOST':'',
                 'PORT':'',
                 },  
             'celery':
                {'ENGINE': 'django.db.backends.sqlite3',
                 'NAME':'celery',
                 'USER':'',
                 'PASSWORD':'',
                 'HOST':'',
                 'PORT':'',
                },  
}

Created a Router Object in db_routers.py file

class CeleryRouter(object):
    """
    This class will route all celery related models to a»
    separate database.
    """

    # Define the applications to be used in the celery database
    APPS = (
        'django',
        'djcelery'
    )

    # Define Database Alias
    DB = 'celery'

    def db_for_read(self, model, **hints):
        """
        Point read operations to celery database.
        """
        if model._meta.app_label in self.APPS:
            return self.DB
        return None

    def db_for_write(self, model, **hints):
        """
        Point write operations to celery database.
        """
        if model._meta.app_label in self.APPS:
            return self.DB
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow any relation between two objects in the db pool
        """
        if (obj1._meta.app_label is self.APPS) and \
           (obj2._meta.app_label in self.APPS):
            return True
        return None

    def allow_syncdb(self, db, model):
        """
        Make sure the celery tables appear only in celery
        database.
        """
        if db == self.DB:
            return model._meta.app_label in self.APPS
        elif model._meta.app_label in self.APPS:
            return False
        return None

Updated the DATABASE_ROUTER variable in settings.py file

DATABASE_ROUTERS = [
    'appname.db_routers.CeleryRouter',
]

Now, when i do python manage.py syncdb i see that the tables are created for celery but there is only one database created i.e. devel. Why are the tables being created in the devel database and not in celery database ?

1 Answer 1

2

Quote from Django docs:

The syncdb management command operates on one database at a time. By default, it operates on the default database, but by providing a --database argument, you can tell syncdb to synchronize a different database.

Try running:

./manage.py syncdb --database=celery
Sign up to request clarification or add additional context in comments.

1 Comment

That works. So this means we'll explicitly need to run syncdb for each database ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.