citusdata / citusdata/django-multitenant
Issue with model inheritance and deletion: Can only delete from one table at a time
- Dominant language
- Python
- Stars
- 823
- Forks
- 126
- PR merge metrics
- No merged PRs in 30d
Description
Hi! First off, thank you for building this library, we were quite excited to get started using it, however, we've run into an issue.
**Issue descripion**
We have our models structured like this:
```
class Tenant(TenantModelMixin):
name = models.CharField(max_length=40)
tenant_id = 'id'
objects = TenantManager()
class Item(TenantModel):
name = models.CharField(max_length=255)
tenant = TenantForeignKey(Tenant, on_delete=models.PROTECT)
tenant_id = 'tenant_id'
class SideItem(Item):
overlay_photo = models.ForeignKey(Photo, on_delete=models.PROTECT)
```
SideItem's primary key is `item_ptr_id`, which is the primary key of Item model. Due to how the models are structured, `tenant` field for SideItem exists in Item's table.
Everything works fine until we try to delete a SideItem (one, or many, or all – that is irrelevant):
```
SideItem.objects.all().delete()
```
We get the following error:
```
File "/Users/llostris/Code/web/app/hubs/tests/test_bags_for.py", line 1090, in test_repeated_calls
Item.objects.all().delete()
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/query.py", line 722, in delete
deleted, _rows_count = collector.delete()
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django_multitenant/query.py", line 77, in delete
return base_delete(obj)
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/deletion.py", line 332, in delete
count = query.delete_batch(pk_list, self.using)
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/sql/subqueries.py", line 41, in delete_batch
num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using)
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/sql/subqueries.py", line 24, in do_query
cursor = self.get_compiler(using).execute_sql(CURSOR)
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1144, in execute_sql
sql, params = self.as_sql()
File "/Users/llostris/Virtualenvs/app/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1430, in as_sql
"Can only delete from one table at a time."
AssertionError: Can only delete from one table at a time.
```
**Libraries used**
- django 3.0.11,
- django-multitenant 2.3.2.
Our database is not distributed.
**Question:**
Is this a bug, or is that type of model inheritance simply not supported with django-multitenant?
Or maybe there is a specific way to use the library with models structured like this, and we've just set things up incorrectly?
Contributor guide
Assessment
This issue has not been assessed yet.