django-auditlog icon indicating copy to clipboard operation
django-auditlog copied to clipboard

Errors with Celery and Django-Tenants

Open stonesoft-tech opened this issue 3 years ago • 5 comments

I am using this with Celery and Django-tenants, however changes made to a tracked model inside a celery task fail with the following error (from Celery):

ProgrammingError('relation "auditlog_logentry" does not exist\nLINE 1: INSERT INTO "auditlog_logentry"

I suspect this is because it is trying to update the auditlog table in the 'public' schema.

For other database related tasks performed in a Celery task, I have to use method supplied with django-tenants:

with schema_context(schema_name):
    # All commands here are ran under the schema `schema_name`

Is there a way that I can have the auditlog database updates respect the correct schema when invoked from a Celery task? similar to the: with set_actor(actor): there coud be

with set_schema(schema_name):

Thanks!

stonesoft-tech avatar Oct 09 '22 18:10 stonesoft-tech

I am not very familiar with Django-tenants. Do you have multiple auditlog_logentry tables? one for each tenant? or one auditlog_logentry for the whole application?

hramezani avatar Oct 09 '22 19:10 hramezani

Thanks for the quick response! The database consists of a postgres schema for each tenant, and each schema contains all of the tables specific to that tenant. So one auditlog_logentry table for each tenant. The schema is usually derived from the hostname, eg. tenant1.webapp.com, tenant2.webapp.com. But in the case of celery tasks, which do not come from a 'request', the schema must be specified.

stonesoft-tech avatar Oct 09 '22 20:10 stonesoft-tech

I should add, there is also a 'public' schema, which is one that can be accessed by all tenants. If no schema is specified it is this schema that is used. And this one does not have the auditlog_entry table.

stonesoft-tech avatar Oct 09 '22 20:10 stonesoft-tech

Thanks for your update. There is no schema change support in django-auditlog and you have to use set_schema context manager.

hramezani avatar Oct 09 '22 21:10 hramezani

Given that (a) django-auditlog doesn't handle the celery tasks itself and (b) it doesn't imply using django-tenants or other multi-tenant / multi-schema tools, the theoretical set_schema context manager shouldn't live in django-auditlog code base.

aleh-rymasheuski avatar Oct 10 '22 09:10 aleh-rymasheuski