Is the database used by django subqueries sticky?
<p>I am having a problem with django subqueries. When I fetch the original QuerySet , I specify the database that I need to use. My hunch is that the later subquery ends up using the 'default' database instead of what the parent query used.</p>

<p>My models approximately look like so (I have several):-</p>

<pre><code>class Author(models.Model):
    author_name=models.CharField(max_length=255)
    author_address=models.CharField(max_length=255)

class Book(models.Model):
    book_name=models.CharField(max_length=255)
    author=models.ForeignKey(Author, null = True)
</code></pre>

<p>Now I fetch a QuerySet representing all books that are called Mark like so:-</p>

<pre><code>b_det = Book.objects.using('some_db').filter(book_name = 'Mark')
</code></pre>

<p>Then later somewhere in the code I trigger a subquery by doing something like:-</p>

<pre><code>if b_det:
    auth_address = b_det[0].author.author_address
</code></pre>

<p>My problem is that arbitrarily in some cases , on my live server, the subquery fails even though there is valid data for that author's id. My suspicion is that the subquery is not making use of the same database 'some_db'. Is this possible? Is it so that the database that needs to be used is not sticky in subqueries? It is just a hunch that this might be a problem, it is happening in the context of a celery worker, is it possible that the combination of celery with django ORM has some bug?  </p>

<p>I have solved this each time this occurred by doing a full fetch by invoking <em>select_related</em> like so.</p>

<pre><code>b_det = Book.objects.using('some_db').select_related('author').filter(book_name = 'Mark')
</code></pre>

<p>So right now, the only way for me to solve the problems is determine beforehand all the data that I will need, and make sure that the top level fetch has all those inner model references using <em>select_related</em>. Any ideas why something like this would fail?</p>

<p>I am unable to recreate this locally else I would have debugged it. Like I said, it is pretty random.</p>