I'm trying to setup two databases for my django app. The first is used for users and the second for any type of user content. The app is dockerized via docker compose.
Here's the docker-compose.yaml file
version: '3'
services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=test
ports:
- "5432:5432"
restart: always
mongodb:
image: mongo:latest
command: ["--bind_ip_all"]
ports:
- "27017:27017"
restart: always
django:
build: .
command: bash -c "
python service_platform/manage.py makemigrations &&
python service_platform/manage.py migrate &&
python service_platform/manage.py runserver 0.0.0.0:8000
"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- postgres
- mongodb
links:
- postgres
- mongodb
Below are the db settings of django's settings.py
DATABASE_ROUTERS = [
'content_manager.content_db_router.ContentDBRouter',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'postgres',
'PASSWORD': 'test',
'PORT': 5432,
},
'content': {
'ENGINE': 'djongo',
'NAME': 'mongodb',
'CLIENT': {
'host': 'mongodb',
'port': 27017,
},
}
}
Here's the custom router I wrote
class ContentDBRouter(object):
content_db = 'content'
default_db = 'default'
def db_for_read(self, model, **hints):
if model._meta.app_label == 'content_manager':
return self.content_db
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'content_manager':
return self.content_db
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if db == self.content_db:
return True
elif db == self.default_db:
return True
else:
raise Exception(f'DB with id {db} not found!')
and finally this is the model I want to get migrated to the mongo db
class Post(models.Model):
id = models.AutoField(primary_key=True)
title = models.TextField(default='default title')
When I try to insert post into the db I get the following error rendered in the browser
MigrationError at /
Table content_manager_post does not exist in database
and the following error in the terminal running the containers with the dbs and the django app.
raise MigrationError(f'Table {collection} does not exist in database')
Can I get some help to properly configure mongo and postgres in order to use them. Initially I tried to run the app without default db and custom routers for each of the models I have, but then reverted to this state in order to properly configure one non default db. I also tried wiping out the whole docker volumes and migrations and run it from scratch in order to apply migrations, but I still didn't manage to get it work.