citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
721 stars 119 forks source link

The create() method does not fill in tenant id #3

Closed begriffs closed 5 years ago

begriffs commented 7 years ago

In my app Store is the tenant. Trying to create a product

s = Store.objects.get(id=1)
set_current_tenant(s)
Product.objects.create(name="nice product", description="buy me", quantity = 1)

The result is

django.db.utils.DataError: cannot perform an INSERT with NULL in the partition column
gilsonbp commented 6 years ago

@begriffs you need a MIDDLEWARE like this to identify the user's tenant but it will only serve for queries:

captura de tela 2017-12-19 as 11 27 23

To add a new record you will need to inform the current tenant: Product.objects.create(name="nice product", description="buy me", quantity = 1, tenant_id=get_current_tenant())