Corvia / django-tenant-users

Adds global user authentication and tenant-specific permissions to django-tenants.
https://django-tenant-users.rtfd.io
MIT License
358 stars 67 forks source link

django.db.utils.ProgrammingError: relation "accounts_client" does not exist #52

Closed Vineet-Sharma29 closed 4 years ago

Vineet-Sharma29 commented 4 years ago

I am trying to create tenant account and have following model:-

import uuid
from django.db import models
from tenant_users.tenants.models import TenantBase
from django_tenants.models import DomainMixin

class Company(models.Model):
    company_id = models.UUIDField(
        default=uuid.uuid4, editable=False, primary_key=True)
    company_name = models.CharField(max_length=50)
    no_of_employees = models.IntegerField(default=0)

class Domain(DomainMixin):
    company = models.OneToOneField(Company, on_delete=models.CASCADE)

class Client(TenantBase):
    id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
    start_date = models.DateTimeField(auto_now_add=True)
    user = models.OneToOneField(
        "users.User", on_delete=models.CASCADE, related_name='customer')
    plan = models.OneToOneField("plan.Plan", on_delete=models.CASCADE)
    device = models.ForeignKey(
        "device.Device", null=True, on_delete=models.CASCADE)
    domain = models.OneToOneField(Domain, on_delete=models.CASCADE)

and

import uuid
from django.db import models

# Create your models here.

class Device(models.Model):
    device_id = models.UUIDField(
        default=uuid.uuid4, editable=False, primary_key=True)
    ip = models.GenericIPAddressField(null=True)
    wires = models.IntegerField(default=0)

When I try to run ./manage.py migrate_schemas --tenant I am getting following error:-

Traceback (most recent call last):
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "accounts_client" does not exist
LINE 1: SELECT "accounts_client"."schema_name" FROM "accounts_client...
                                                    ^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django_tenants/management/commands/migrate_schemas.py", line 63, in handle
    executor.run_migrations(tenants=tenants)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django_tenants/migration_executors/standard.py", line 8, in run_migrations
    tenants = tenants or []
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/models/query.py", line 280, in __bool__
    self._fetch_all()
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/models/query.py", line 184, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1103, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
    cursor.execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_client" does not exist
LINE 1: SELECT "accounts_client"."schema_name" FROM "accounts_client...

Also I try to run ./manage.py migrate_schemas --shared, I am getting following error:-

[standard:public] === Starting migration
[standard:public] Operations to perform:
[standard:public]   Apply all migrations: accounts, admin, auth, contenttypes, device, permissions, plan, sessions, users
[standard:public] Running migrations:
[standard:public]   Applying device.0001_initial...
[standard:public]  OK
[standard:public]   Applying accounts.0001_initial...
Traceback (most recent call last):
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "device_device" does not exist

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django_tenants/management/commands/migrate_schemas.py", line 49, in handle
    executor.run_migrations(tenants=[self.PUBLIC_SCHEMA_NAME])
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/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 "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django_tenants/migration_executors/base.py", line 36, in run_migrations
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/migrations/executor.py", line 247, in apply_migration
    migration_recorded = True
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 115, in __exit__
    self.execute(sql)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/vineet/projects/env-cosgrid/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "device_device" does not exist

I have following in settings.py:-

"""
Django settings for cosgrid project.

Generated by 'django-admin startproject' using Django 3.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

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

    'tenant_users.permissions',
    'tenant_users.tenants'
    'accounts',
    'users',
    'plan'
]

TENANT_APPS = [
    'device',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'tenant_users.permissions',
]

INSTALLED_APPS = list(SHARED_APPS) + \
    [app for app in TENANT_APPS if app not in SHARED_APPS]

MIDDLEWARE = [
    'django_tenants.middleware.main.TenantMainMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'cosgrid.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'cosgrid.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django_tenants.postgresql_backend',
        'NAME': '***',
        'USER': '***',
        'PASSWORD': '***',
        'HOST': '***',
        'PORT': '***'
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

SESSION_COOKIE_DOMAIN = '.mydomain.com'

TENANT_USERS_DOMAIN = "example.com"

AUTH_USER_MODEL = 'users.User'

TENANT_MODEL = 'accounts.Client'

TENANT_DOMAIN_MODEL = 'accounts.Domain'

AUTHENTICATION_BACKENDS = (
    'tenant_users.permissions.backend.UserBackend',
)

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

Could not figure out why this is happening. Is it a issue with django-tenant-users because I have seen similar unsolved errors on stackoverflow which are trying to implement multi-tenancy in django

Viatrak commented 4 years ago

This does not appear to be an issue with DTU.