django-tenant-schemas icon indicating copy to clipboard operation
django-tenant-schemas copied to clipboard

Migration issue when deleting a tenant model that's (generically?) related to another model

Open john2x opened this issue 8 years ago • 6 comments

We ran into an issue when deleting a model's table. I prepared a repo that reproduces the issue. It is using django-taggit, but I guess the issue also happens with other generic relationships? The problem doesn't happen when using ForeignKeys.

Basically, we have a model that uses taggit.TaggableManager and has tags related to it. This is the ToBeDeletedModel.

Note that taggit is listed in the TENANT_APPS list.

For the first step (Step A), we create some test data for the model and add tags to it (for example).

For the next step (Step B), we introduce a migration that deletes ToBeDeletedModel and its table. Running manage.py migrate_schemas fails with the following stacktrace:

=== Running migrate for schema public
Operations to perform:
  Apply all migrations: customers, sessions, admin, contenttypes, auth, taggit, shared_app
Running migrations:
  Rendering model states... DONE
  Applying shared_app.0002_delete_model... OK
The following content types are stale and need to be deleted:

    shared_app | tobedeletedmodel

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  # trimmed
django.db.utils.ProgrammingError: relation "taggit_taggeditem" does not exist
LINE 1: DELETE FROM "taggit_taggeditem" WHERE "taggit_taggeditem"."c...

It seems it is attempting to delete the taggit table in the public schema, even though taggit is listed in the TENANT_APPS list.

The repo contains a README on how to setup and reproduce the issue.

john2x avatar May 17 '16 06:05 john2x

+1

filipeximenes avatar May 25 '16 23:05 filipeximenes

@john2x did you manage to fix this?

filipeximenes avatar Jun 17 '16 17:06 filipeximenes

Haven't found a fix or workaround for it. But I did find that the cause is definitely due to apps being listed in both SHARED_APPS and TENANT_APPS.

john2x avatar Jun 18 '16 01:06 john2x

@john2x Ran this manually and it solves the problem

c = ContentType.objects.get(model='to_be_deleted_model')
with schema_context('schema_name'):
    c.delete()

So it's confirmed that it's incorrectly trying to access the public schema.

filipeximenes avatar Jun 21 '16 15:06 filipeximenes

Actually, I thought it was failing for all schemas, but now I get it only fails for public. So not much of a progress here.

filipeximenes avatar Jun 21 '16 15:06 filipeximenes

Apps from TENANT_APPS being referenced in public schema's auth_permission table.

Is there any update to this issue?? I'm facing same problem while trying to delete user(auth model) from the main site(in the public schema). Actually, i've to implement multiple types of user in the tenant schemas.

My settings.py look like:

SHARED_APPS = [
    'tenant_schemas',
    'Client',

    # for admin related stuffs
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'common_auth_user',


    'rest_framework',

]

TENANT_APPS = [

    # authentication related stuffs
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'common_auth_user', # holds auth user model

    'rest_framework',


    'app_level_user_types', # using foreign key to map to auth user
]

The problem is, every model from app_level_user_type app is being added to the permission tables(auth_permissions) on public schema, though they are meant to be in the tables of tenant specific schemas.

sbhusal123 avatar Apr 04 '20 22:04 sbhusal123