Creating an object with more than 1 foreign key in a Django project with multiple databases causing error
<p>In my Django project, I have 2 databases. Everything works perfectly except for when I try to create a model with 2 foreign keys. The database router locks up and gives me a <code>Cannot assign "&lt;FKObject: fk_object2&gt;": the current database router prevents this relation.</code> even if both foreign keys come from the same database and I've yet to save the object in question. Code below</p>

<pre><code>1 fk_object1 = FKObject.objects.using("database2").get(pk=1)
2 fk_object2 = FKObject.objects.using("database2").get(pk=2)
3
4 object = Object()
5 object.fk_object1 = fk_object1
6 object.fk_object2 2 = fk_object2
7 object.save(using="database2")
</code></pre>

<p>The problem arises on line 6, before the object is even saved into the database so I'm assuming that Django somehow calls <code>Object()</code> with <code>database1</code> even though it hasn't been specified yet.</p>

<p>Does anyone know how to deal with this?</p>

<p>So I ended up finding a work around as such:</p>

<p>As it turns out, my suspicions were only partially true. Calling <code>Model()</code> does not cause Django to assume it is to use the default database but setting a foreign key does. This would explain why my code would error out at line 6 and not at line 5 as by this point in time, Django already assumes that you're using the default database and as <code>fk_object2</code> is called from <code>database2</code>, it errors out for fear of causing an inter-database relation.</p>

<p>To get around this, I used <code>threading.current_thread()</code> as so:</p>

<pre><code>class Command(BaseCommand):
    current_thread().db_name = "database2"

    def handle(self, **args, **kwargs):
        # Do work here


class DatabaseRouter(object):
    db_thread = threading.current_thread()

    def db_for_read(self, model, **hints):
        try:
            print("Using {}".format(db_thread.db_name))
            return db_thread.db_name
        except AttributeError:
            return "default"

    def db_for_write(self, model, **hints):
        try:
            print("Using {}".format(db_thread.db_name))
            return db_thread.db_name
        except AttributeError:
            return "default"
</code></pre>

<p>This way, my 2nd database is used every time, thereby avoiding any possible relation inconsistencies.</p>

python django python-3.x django-models