Search code examples
djangodjango-modelsmulti-tenantdjango-celery-beat

Deploying a multi tenant application (django-tenant) - Error to run ./manage migrate_schemas


I have a single django application hosted on AWS - Web client <=> nginx <=> uwsgi <=> django. I decided to transform It into a multi tenant with django-tenant. Also, I'm using django-celery-beat for scheduling tasks. My single application works normally on AWS and my multi tenant works locally too, in my machine. I had a problem recognizing the schemas with celery, but I solved It here: Is It possible to user django-celery-beat with django-tenant?. However, the error I'm getting now is within my VPN: django.db.utils.ProgrammingError: relation" app_modelcustomuser "does not exist

It appears when I try to run ./manage migrate_schemas (I do makemigration in my local machine and commit It, so I just need to migrate to the DB in my VPN) or any other migrate. I tried to migrate by application and I get It when I do ./manage migrate admin. My settings.py file looks like this:

SHARED_APPS = [
    'django_tenants',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'clients',
]

TENANT_APPS = [
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    # Libs
    'stdimage',
    'django_celery_beat',
    'djcelery_email',
    'tenant_schemas_celery',
    # 'widget_tweaks',
    'bootstrap4',
    'bootstrapform',
    mathfilters,
    'django_filters',
    'sorl.thumbnail',
    # Apps
    'presence',
    'confirmed list',
    'topper',
    'app_users.apps.AppUsersConfig',
]

INSTALLED_APPS = list (set (SHARED_APPS + TENANT_APPS))

AUTH_USER_MODEL = 'app_users.ModelCustomUser'

PUBLIC_SCHEMA_URLCONF = 'myproject.urls_public'

# Django Tenant
TENANT_MODEL = "clients.Client"  # app.Model

TENANT_DOMAIN_MODEL = "clients.Domain"  # app.Model

DATABASE_ROUTERS = (
    'django_tenants.routers.TenantSyncRouter',
)

my models.py (app application):

class ModelCustomUser (AbstractUser):
    image = ImageField (
        'Image',
        upload_to = 'images',
        default = 'images/new_logo.png',
    )

my forms.py:

class CustomUserCreationForm (UserCreationForm):
    class Goal:
        model = ModelCustomUser
        # Changed when placing the Filters
        fields = ('first_name', 'last_name', 'username', 'email', 'image')


class CustomUserChangeForm (UserChangeForm):
    class Goal:
        model = ModelCustomUser
        fields = ('first_name', 'last_name', 'username', 'email', 'image')

My admin.py:

class AdminCustomUser (UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = ModelCustomUser
    # list_display = ['first_name', 'last_name', 'email', 'username', 'image']
    list_display = ['first_name', 'last_name', 'email', 'username', 'image']


admin.site.register (ModelCustomUser, AdminCustomUser)

In my Clients Application, my models.py:

class Client(TenantMixin):
    name = models.CharField(max_length=100)

    # default true, schema will be automatically created and synced when it is saved
    auto_create_schema = True

    def __str__(self):
        return self.name


class Domain(DomainMixin):
    pass

I've tried to correct this problem for a long time, but I've not been successful. If anyone can help me, I really appreciate It!


Solution

  • My error was a beginner error. When I had deployed my single application, the database had not changed, I left db.sqlite3. When I switched to multi tenant, I had to switch to PostgreSQL. My problem was because I needed to run:

    python manage.py collectstatic