Code Monkey home page Code Monkey logo

Comments (11)

kissgyorgy avatar kissgyorgy commented on June 17, 2024

Do you have a tenant with the same domain where your request is going?
For example, do you have a host with domain_name "first.example.com" when you get the page from http://first.example.com ?

from django-tenant-schemas.

jjimenezlopez avatar jjimenezlopez commented on June 17, 2024

No, i didn't. I was creating a tenant with domain_url "client1.example.com" from the public schema in "example.com". And that was the first tenant (also failed with second and third tenant), so I didn't have any tenant with that url.

If I use my fork, from a few weeks ago (https://github.com/jjimenezlopez/django-tenant-schemas), the error does not occurs.

from django-tenant-schemas.

kissgyorgy avatar kissgyorgy commented on June 17, 2024

Did you successfully create the public schema with every table synced before the client1 tenant?

from django-tenant-schemas.

jjimenezlopez avatar jjimenezlopez commented on June 17, 2024

Yes, the public schema was created correctly and without errors. Then, when I tried to create the tenant schema, it was created and all tables were synced, but gave me that error after that.

from django-tenant-schemas.

kissgyorgy avatar kissgyorgy commented on June 17, 2024

Check if the changes in #62 have any effect on this behavior. We basically restricted the tenant_creation into public tenant and modification into public and current tenant's domain, but I don't this change is causing this, but might worth a look.

from django-tenant-schemas.

bernardopires avatar bernardopires commented on June 17, 2024

Sorry for the ultra late reply. It looks like you have overwritten the
save() method on your tenant model, as there is no call to admin_user
on this app's code. Is this correct? Can you please post the code of this
method here? This will give us the insight needed to fix your problem.

2013/9/4 Walkman [email protected]

Check if the changes in #62https://github.com/bcarneiro/django-tenant-schemas/issues/62have any effect on this behavior. We basically restricted the
tenant_creation into public tenant and modification into public and current
tenant's domain, but I don't this change is causing this, but might worth a
look.


Reply to this email directly or view it on GitHubhttps://github.com//issues/65#issuecomment-23789894
.

Bernardo Pires Carneiro

from django-tenant-schemas.

jjimenezlopez avatar jjimenezlopez commented on June 17, 2024

Hello @bcarneiro, thanks for your reply.

Here is my tenant save method:

    def save(self, admin_email=None, *args, **kwargs):

        if self.pk is None:
            # se está añadiendo
            self.schema_name = self.domain_url

        self.domain_url = '%s.%s' % (self.domain_url, settings.APP_DOMAIN)

        super(Customer, self).save(*args, **kwargs)

        if admin_email is not None:
            # creamos el usuario administrador del cliente
            with tenant_context(self):
                mail_edited = False
                new_password = generate_password()
                try:
                    admin_user = User.objects.get(username="admin")
                    if admin_email != admin_user.email:
                        mail_edited = True
                        admin_user.email = admin_email
                except User.DoesNotExist:
                    admin_user = User(username="admin", email=admin_email, is_staff=True)

                if mail_edited or admin_user.pk is None:
                    admin_user.set_password(new_password)
                    subject = _("my_project")
                    body = _(u"Ha sido registrado en my_project, su nuevo usuario es ")
                    body += "admin"
                    body += _(u" y su clave ")
                    body += new_password
                    send_mail(subject, body, settings.EMAIL_HOST_USER, [admin_email], fail_silently=True)

                admin_user.save()

Far as I understand, the code in the save method should not affect.

Thanks!

from django-tenant-schemas.

bernardopires avatar bernardopires commented on June 17, 2024

Alright, I think I know where the problem is. Your code is perfect Jose,
you don't have to change anything. This is a mistake in this app.

So here's what's happening and this is why it only started happening after
#62.

Jose uses with tenant_context(self):, which basically sets the
search_path to the given tenant on the with's scope and returns to the
previous schema when the with ends. tenant_context saves the current
tenant in previous_tenant, changes the search_path to the given tenant
and then calls connection.set_tenant(previous_tenant) at the end. The
problem in this case is that the current schema is public
because set_schema_to_public is called at the end of the save method.
set_schema_to_public sets self.tenant = None, so previous_tenant will
be None inside tenant_context. tenant_context then calls on the
end connection.set_tenant(previous_tenant) and fails. So the problem is,
tenant_context doesn't work if the current schema is public. This is
also why it only started happening after #62, as previously
previous_schema would be the tenant that was just created.

A quick fix could be to change the function set_schema_to_public to
actually fetch the public tenant.

    def set_schema_to_public(self):
        self.tenant = get_tenant_model().objects.get(schema_name=get_public_schema_name())
        self.schema_name = get_public_schema_name()

or to instantiate a fake public tenant (I'm not sure if this is going to
work, maybe we'll have to use TenantMixin instead. Important here is to
NOT save this fake instance)

    def set_schema_to_public(self):
        self.tenant = get_tenant_model()(schema_name=get_public_schema_name())
        self.schema_name = get_public_schema_name()

Thoughts? I'd be very grateful if someone could test and fix this bug!

Cheers,
Bernardo

from django-tenant-schemas.

jjimenezlopez avatar jjimenezlopez commented on June 17, 2024

Thanks for your help @bcarneiro, I will try it later.

from django-tenant-schemas.

jjimenezlopez avatar jjimenezlopez commented on June 17, 2024

I have been trying to fix this. For any reason I don't know, the get_tenant_model() method returns None when is called from postgresql_backend.base.py.

But I think I have found another solution. I have modified the tenant_context method:

@contextmanager
def tenant_context(tenant):
    previous_tenant = connection.get_tenant()

    # if previous_tenant is None, means that we are working with the public schema
    if previous_tenant is None:
        previous_tenant = get_tenant_model().objects.get(schema_name=get_public_schema_name())

    try:
        connection.set_tenant(tenant)
        yield
    finally:
        connection.set_tenant(previous_tenant)

What do you think guys? I think it makes sense.

from django-tenant-schemas.

bernardopires avatar bernardopires commented on June 17, 2024

Hi Jose,

thanks for the help! I don't like this solution of fixing it on
tenant_context, I think it doesn't really solve the problem in the right
way and schema_context would also have the exact same problem. A tenant
should always be available, so it needs to be set on set_schema_to_public.
I'll take a look later in the code if noone fixes it.

2013/9/10 Jose Jiménez [email protected]

I have been trying to fix this. For any reason I don't know, the *
get_tenant_model()* method returns None when is called from *
postgresql_backend.base.py*.

But I think I have found another solution. I have modified the *
tenant_context* method:

@contextmanagerdef tenant_context(tenant):
previous_tenant = connection.get_tenant()

# if previous_tenant is None, means that we are working with the public schema
if previous_tenant is None:
    previous_tenant = get_tenant_model().objects.get(schema_name=get_public_schema_name())

try:
    connection.set_tenant(tenant)
    yield
finally:
    connection.set_tenant(previous_tenant)

What do you think guys? I think it has sense.


Reply to this email directly or view it on GitHubhttps://github.com//issues/65#issuecomment-24172540
.

Bernardo Pires Carneiro

from django-tenant-schemas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.