Shashishekhar Hasabnis
Shashishekhar Hasabnis

Reputation: 1756

If using Django-tenat-schemas how to flush database?

I am using django-tenant-schemas app to handle tenants and database related to various tenants. Now what I want to do is flush all the database so how can this be done?

Upvotes: 1

Views: 814

Answers (2)

markus-hinsche
markus-hinsche

Reputation: 1422

I am working with https://github.com/tomturner/django-tenants, but this should also work for https://github.com/bernardopires/django-tenant-schemas.

When using multiple tenants/schemas, flush does not work anymore. When you use manage.py flush, the error tells you that there are foreign key constraints that are not met; and it suggests:

HINT:  Truncate table "<your table name>" at the same time, or use TRUNCATE ... CASCADE.

Here is a management command (flush_cascade.py) which does that (I only tested with PostgreSQL`):

from django.core.management import call_command
from django.core.management.commands.flush import Command as FlushCommand
from django.db import transaction

class Command(FlushCommand):    
    @transaction.atomic
    def handle(self, *args, **options):
        options['allow_cascade'] = True
        call_command('flush', *args, **options)

Django's default flush command only sends TRUNCATE to the database. It doesn't add CASCADE because not all database backends support cascading statements.

Upvotes: 3

Hedde van der Heide
Hedde van der Heide

Reputation: 22459

You can use any django command on a per-schema basis using the tenant_command wrapper, if you want to iterate all schema's you'll have to create a custom command that iterates all tenants

./manage.py tenant_command flush --schema=customer1

source: http://django-tenant-schemas.readthedocs.io/en/latest/use.html#tenant-command

Upvotes: 2

Related Questions