Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom command ignoring tenant #114

Closed
perrohunter opened this issue Feb 12, 2017 · 3 comments
Closed

Custom command ignoring tenant #114

perrohunter opened this issue Feb 12, 2017 · 3 comments

Comments

@perrohunter
Copy link
Contributor

perrohunter commented Feb 12, 2017

Hi, I'm writing a custom command and I'm realizing that even if I specify a --schema when running the custom command with tenant_command some times I'm getting error:

django.db.utils.ProgrammingError: relation "my_table" does not exist
LINE 1: SELECT (1) AS "a" FROM "my_table" WHERE "my_tabl...

I've notice that this happens when I'm trying to access a tenant app and suddenly some models are not properly tenant configured and throw the error.

Any away I can ensure the right schema is properly configured?

This only happens when running code through a tenant_command where I'm loading modules dynamically and one of them causes an exception.

My Logic:

  1. run ./manage.py tenant_command demodata
    my demodata command looks through all the django applications, find the ones containing a demo folder and loads each file in it the calls a install() method sample gist

  2. demodata command files two files cats.py and dogs.py, it loads them and then calls a method first on cats.py then dogs.py

  3. cats.py fails with an exception "integrity bla bla, duplicate key" so far, everything is as expected

  4. dogs.py gets run and upon executing the first db query Dogs.objects.filter(goodBoy=True) it fails with

django.db.utils.ProgrammingError: relation "pets_dogs" does not exist
LINE 1: SELECT (1) AS "a" FROM "pets_dogs" WHERE "pets_dog...
                               ^

this is a really weird scenario, if no exception is caused by cats.py then dogs.py runs just fine

@perrohunter
Copy link
Contributor Author

So I found the cause, for some reason when I cause a exception on the database side, let's say an Integrity Error which raises a django.db.utils.IntegrityError for some reason a new cursor gets created on the django side, this new cursor doesn't have the proper search_path but the _cursor() funcion on base.py things it does and therefore it doesn't re-set the search_path a way around this is to monitor if the cursor we are getting is different from the previous cursor we got.

New attribute of the DatabaseWrapper

_previous_cursor = None

and inside _cursor() validation of search_path

if (not get_limit_set_calls()) or not self.search_path_set or self._previous_cursor != cursor:
            self._previous_cursor = cursor

perrohunter pushed a commit to perrohunter/django-tenants that referenced this issue Feb 13, 2017
perrohunter added a commit to perrohunter/django-tenants that referenced this issue Feb 13, 2017
@tomturner
Copy link
Member

@perrohunter Thank you I will have a look at the pull request

tomturner added a commit that referenced this issue Feb 22, 2017
@tomturner
Copy link
Member

Merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants