Django queryset "using()" method for specifying database doesn't work on related_name queries
<p>I've noticed something strange in Django that I haven't been able figure out. Let's say I have a Django application (I'm using 1.7) with two models like so:</p>

<pre><code>class Bookstore(models.Model):
    name = models.CharField(max_length=50)

class Book(models.Model):
    title = models.CharField(max_length=100)
    store = models.ForeignKey(Bookstore, on_delete=models.PROTECT, related_name='books')
</code></pre>

<p>And then let's say I have two databases storing the data, perhaps a master-replica setup -- let's say the master is denoted as <code>default</code> in my settings file and the replica is <code>replica</code>. If I do the following query, I get an exception saying</p>

<blockquote>
  <p>the current database router prevents this relation</p>
</blockquote>

<pre><code>store = Bookstore.objects.using('default').get(id=1)
first_book = store.books.using('replica').all().order_by('id')[0]
</code></pre>

<p>However, the following, which should be the same query, works just fine:</p>

<pre><code>store = Bookstore.objects.using('default').get(id=1)
first_book = Book.objects.using('replica').filter(store=store).order_by('id')[0]
</code></pre>

<p>What's going on here? Is there anyway to use the related_name lookup like the first example but have it work properly? Thanks!</p>

<p>Look at SQL from first example (without <code>using()</code>):</p>

<pre><code>SELECT "book"."id", "book"."title", "book"."store_id"
FROM "book" JOIN "bookstore" ON "book"."store_id" = "bookstore"."id"
WHERE "bookstore"."id" = 1
ORDER BY "book"."id"
LIMIT 1
</code></pre>

<p>Do You see the problem? You cannot do a <code>SELECT</code> on two databases.</p>

<p>And look at SQL from second example:</p>

<pre><code>SELECT "book"."id", "book"."title", "book"."store_id"
FROM "book"
WHERE "book"."store_id" = 1
ORDER BY "book"."id"
LIMIT 1
</code></pre>

<p>Select data from one table.</p>

python django