0

I ran into a case when model and fireign key model are both referencing to one DB but DB isn't default.

F.e.

class Ref(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField()

class Main(class.Model):
    id = models.IntegerField()
    ref_id = models.ForeignKey(Ref)
...........

qs = models.Main.object.using('custom_db').all()

Django attemps to get foreign key from default database and expectially catches an exception 'table or view doesnt exist'

Is there a simple way to say to foreign key model use the same database as Main ? Thanks

frett
  • 1
  • I think django doesn't support relationship that span to multiple database. `https://stackoverflow.com/questions/6830564/how-to-use-django-models-with-foreign-keys-in-different-dbs` – Kaushal Aug 09 '17 at 14:49

1 Answers1

0

Change:

class Main(class.Model):
    ...

to

class Main(models.Model):
    ...

As the Main class isn't subclassing a Django model, it's not being created in the database. Hence why you have the Ref table being created in the database, but the table or view doesnt exist for the Main class.

tdsymonds
  • 1,679
  • 1
  • 16
  • 26