Elabbasy00
Elabbasy00

Reputation: 678

Django tenants can't delete record from a shared model that has FK in tenant from public schema

I Have a custom user model shared between tenants and public


HAS_MULTI_TYPE_TENANTS = True
MULTI_TYPE_DATABASE_FIELD = 'type'

TENANT_TYPES = {
    "public": {
        "APPS": [
            'django_tenants',
            'clients',
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'rest_framework',
            'rest_framework.authtoken',
            'djoser',
            "corsheaders",

            'accounts',

        ],
        "URLCONF": "server.urls_public",
    },
    "menu": {
        "APPS": [
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'rest_framework.authtoken',

            'accounts',
            'products',
        ],
        "URLCONF": "server.urls_menu",
    },
    "full-version": {
        "APPS": [
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'rest_framework.authtoken',

            'accounts',
            'products',
            'order',
  
        ],
        "URLCONF": "server.urls_full",
    }
}

INSTALLED_APPS = []
for schema in TENANT_TYPES:
    INSTALLED_APPS += [app for app in TENANT_TYPES[schema]
                       ["APPS"] if app not in INSTALLED_APPS]

from the full-version order app, there is an FK to the user model that restrict me to delete any user from the public schema


 class Address(models.Model):
    user = models.ForeignKey(
        User, on_delete=models.SET_NULL, null=True, blank=True)

also, the issue appears in the menu type cuz the order models do not exist yet


 django.db.utils.ProgrammingError: relation "order_address" does not exist
  LINE 1: ...."user_type", "accounts_user"."phone_number" FROM "order_add...

Is any work around or do I have to create an empty model for each type?

Upvotes: 0

Views: 229

Answers (1)

St3pa
St3pa

Reputation: 1

Well I guess the issue here is that you are updating with NULL even tho it won't be available, the solution is to do nothing in this case replace

on_delete=models.SET_NULL, null=True, blank=True)

to

on_delete=models.DO_NOTHING, null=True, blank=True)

Upvotes: 0

Related Questions