How to set search paths for Heroku PostgreSQL database?
I'm currently deploying a Django app to Heroku, and my settings.py
looks like this:
if IS_HEROKU_APP:
DATABASE_URL = dj_database_url.config(
env="DATABASE_URL",
conn_max_age=600,
conn_health_checks=True,
ssl_require=True,
)
else:
DATABASE_URL = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DATABASE_NAME'),
'USER': config('DATABASE_USER'),
'PASSWORD': config('DATABASE_PASSWORD'),
'HOST': config('DATABASE_HOST'),
'PORT': config('DATABASE_PORT'),
}
DATABASES = {
'default': {
**DATABASE_URL,
'OPTIONS': {
'options': '-c search_path=public'
},
},
'modules': {
**DATABASE_URL,
'OPTIONS': {
'options': '-c search_path=modules'
},
},
'alumni': {
**DATABASE_URL,
'OPTIONS': {
'options': '-c search_path=alumni'
},
}
}
DATABASE_ROUTERS = ['MCMT.db_routers.ModulesRouter', 'MCMT.db_routers.AlumniRouter', 'MCMT.db_routers.DefaultRouter']
This works locally but not when I push to Heroku (specifically in the release phase, when I have to migrate). I get the error:
django.db.utils.ProgrammingError: relation "info" does not exist
remote: LINE 1: SELECT "info"."code" FROM "info"
which is because there is no table "info", only "modules.info". I think this is because Heroku failed to see the search_path options. How should I resolve this?
I have tried to create the schemas first using heroku pg:psql
, but that gives me the same error. I also tried to see if it's an issue with dj_database_url but I fail to find any solution online.