django-tenants / django-tenants

Django tenants using PostgreSQL Schemas
MIT License
1.51k stars 342 forks source link

Tenant is not redirecting to correct schema when post request is made #521

Open schults opened 3 years ago

schults commented 3 years ago

First, I'm not sure if it's a problem of the package itself or something I'm doing wrong (I guess it's the second option).

I have a app 'costumers' which is held my tenant model. This app is on 'SHARED_APPS' right under 'django_tenants'. My app 'university' is on 'TENANT_APPS'. This app contains all the logic and it has my models.py.

I managed to create tenants correctly, BUT, the problem occurs when I try to use any of my Api endpoints. Example:

I created a tenant called university1 and this tenant has it's own schema on my PostgreSQL database. When I do a post request using an api (using this tenant 'university1'), django is trying to use a table which doesn't exist on my schema. I really don't know what I'm doing wrong.

In my models.py (in my app 'university'), do I have to add anything there? For example class Meta: managed = False and db_table=name_of_table_on_database?

This is a sample of it:

from django.db import models
from comum.models import *
from datetime import datetime
from django.utils.translation import gettext_lazy as _
from django.db import connection
class AcademicUniversity(models.Model):
    id_unity             = models.AutoField(primary_key=True)
    id_unity_ies       = models.CharField(max_length=100, unique=True, null=True)
    unity_name       = models.CharField(max_length=100, null=True)
    sigla                   = models.CharField(max_length=100, null=True)
    co_localidade   = models.ForeignKey(Localidade, on_delete=models.CASCADE, db_column='co_localidade', related_name='localidade_unidade_academica', null=True)

When the request is made and on my APIView the is_valid method is called, it's raising an exception saying that my relation doesn't exist because django is (somehow) changing the relation name.

This is the query:

DEBUG:django.db.backends:(0.071) SELECT "university_academicunity"."id_unity", "university_academicunity"."id_unity_ies", "university_academicunity"."unity_name", "university_academicunity"."sigla", "university_academicunity"."co_localidade_mec" FROM "university_academicunity" WHERE "university_academicunity"."id_unity_ies" = '1' LIMIT 21; args=('1',)

@tomturner do you know what can I do to fix this ? I really appreciate any help. Thank you very much

moojen commented 3 years ago

@schults When reading through your case, it seems as if the main issue is that the api-routing isn't tenant aware and as such, is unable to update within the correct schema. Secondly, I get the impression that your University app has the details that actually belong within the customer model, because when Universities are your tenants, why would you have a list of Universities for each tenant (University)?

Step 1 would be to see what schema your api is using, when updating. Step 2: check if the location of data makes sense, where what should be updated.

I hope the above helps you!