Django Heroku django-tenant attributeerror: 'databasewrapper' object has no attribute 'schema_name'
I'm migrating my development to django-tenants, I didn´t had any issues doing it on my local, but when I push my changes to the prod environment in Heroku, I'm getting the following error:
attributeerror: 'databasewrapper' object has no attribute 'schema_name'
Procfile
web: gunicorn tlgrmbot.wsgi
Database settings:
DATABASES = {
'default': {
'ENGINE': 'django_tenants.postgresql_backend',
'NAME': os.environ.get('DB_NAME_BOT'),
'USER': os.environ.get('DB_USER_BOT'),
'PASSWORD': os.environ.get('DB_PWD_BOT'),
'HOST': 'localhost',
'PORT': '5432',
}
}
DATABASE_ROUTERS = (
'django_tenants.routers.TenantSyncRouter',
)
TENANT_MODEL = "tenant_manager.Tenant"
TENANT_DOMAIN_MODEL = "tenant_manager.Domain"
and I have this toggle here for prod environment:
if mode == 'webhook':
DATABASES['default'] = dj_database_url.config(
conn_max_age=600, ssl_require=True)
The issue appears when I try to make migrations in the prod environment in Heroku
Error log:
<module 'os' (frozen)>
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.11/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.heroku/python/lib/python3.11/site-packages/django_tenants/management/commands/migrate_schemas.py", line 66, in handle
executor.run_migrations(tenants=[self.PUBLIC_SCHEMA_NAME])
File "/app/.heroku/python/lib/python3.11/site-packages/django_tenants/migration_executors/standard.py", line 11, in run_migrations
run_migrations(self.args, self.options, self.codename, self.PUBLIC_SCHEMA_NAME)
File "/app/.heroku/python/lib/python3.11/site-packages/django_tenants/migration_executors/base.py", line 45, in run_migrations
connection.set_schema(schema_name, tenant_type=tenant_type, include_public=False)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DatabaseWrapper' object has no attribute 'set_schema'
As I think, when you use dj_database_url.config()
in production, it's overriding your django-tenants database engine with the standard PostgreSQL engine. This happens because dj_database_url.config()
doesn't know about the custom django_tenants.postgresql_backend
engine.
So, to solve that, you need to modify your production database configuration to preserve the django-tenants engine.
if mode == 'webhook':
db_config = dj_database_url.config(
conn_max_age=600,
ssl_require=True
)
# Override the engine to use django-tenants backend
db_config['ENGINE'] = 'django_tenants.postgresql_backend'
# Update the DATABASES configuration
DATABASES['default'] = db_config```