-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deleting a tenant or migrating a tenant-only model change causes errors #925
Comments
Got the same problem myself. Keep on stumbling on issues with django-tenants: cannot use subfolders, cannot use asgi, and cannot delete tenants. Python is supposed to be the most popular language of today, but I wonder what these people then do with it, if basic things don't work? I spend 30% of time programming, and 70% debugging errors of someone else. In my case the delete function of my model is called. Since there isn't any, it calls the delete function of the inherited models.py of django_tenants. It then drops the schema, allright. From there is goes super().delete. There it searches correctly for csb_tenants_domain. Then it tries to do a select from a table in the schema that doesn't exist anymore, and looks for that table in the public schema, where it never existed of course. I wonder why it still does a select, in stead of immediately doing a delete of that particular row in the csb_tenants_domain table. Then, I ask myself, why would this result in a hard unhandled exception? Well, I will keep you posted if I have found anything. I have to admit that I start wondering if I shouldn't go back to delphi or c-sharp... |
I found it! And it turns out I can't blame this one on bad coding of django-tenants. Honest is honest, this was my mistake. I had coded my class in the models.py of my tenant app like this: I was ill advised, but there you are. It makes sense to make that you want a foreign key if you want to link this class of the tenant to the list of tenants in the public schema. And then, it also made sense to me that you do the on_delete in cascading. What happens though, is that first the schema is deleted, and then the tenant is deleted. The deletion of the denant goes first through the delete_tenant.py of django_tenants, where a few checks are being done, and then it is being sent to the core of django, who doesn't know anything about schema's. And django does it job and goes looking for this table, of course in the public schema since it doesn't know any better, where it doesn't find it of course. By changing the line into this: @davidoh14 tried with set_null and that didn't help for him. However, using the DO_NOTHING did the trick for me. Now it thoroughly delets the row of the specific tenant in the public schema, it deletes the line of the domain in the public schema, drops the schema of the tenant, and with that all of the tables inside of course. Hence, if you create a class in the models.py of a tenant, use the DO_NOTHING, but if you create a class in the app of the public schema (client app in the example of Tom, and customers class) then you have to create it with the on_delete to CASCADE, because then they will not be automatically dropped by dropping the schema. So, there you are. It took me a couple of hours, but I learned a lot about how Django, the ORM and django_tenants work. Problem solved, what a great feeling! |
I'm having the same issue now. When i try to delete a tenant using
where |
Suppose: If your Tenant model is used by Student model as FK. |
I am able to create tenants and access subdomained django admins just fine, but the errors I'm running into make me think something is wrong with the non-shared side.
Deleting a tenant by the command 'delete_tenant' causes the error in the codeblock below. The schema actually gets deleted in postgres (I am no longer able to see it in pgAdmin), but django does not recognize these changes and shows the model, domain, and schema as existing still.
The only way I can avoid this is by using the python shell, getting the schema_context, deleting the tenant without making auto_drop_schema to true, then deleting the schema itself by executing a connection.cursor SQL command.
I then tried the latter flow to delete, but this time with auto_drop_schema set to true, but that errored out.
The schema gets deleted in pgAdmin, but the tenant model instance and domain still remain in django. When I run delete_tenant again and list out the schemas, the schema is returned in the list of results, despite not showing in pgAdmin.
Why does deleting a tenant in the public schema cause django to look for a table that should only exist in non-shared schemas? I initially suspected that it may be because the website model foreign key is set to cascade deletion from the tenant model, but I tested set_null and still ran into the same issue.
settings.py:
requirements.txt:
The text was updated successfully, but these errors were encountered: