Here’s the scenario I’m faced with:
I’m working on a legacy postgres database which has approximately 80 different schemas, and I want to migrate an existing application that uses that db (and performs hand-made queries) to Django.
I’m following the advice here and I created one Django app per schema, and generated unmanaged models using
inspectdb with a connection per schema that uses the search path option to only look for that schema.
So now I’m finding myself with about 80 django apps, all with a models.py file containing their respective tables.
I need to model some queries that look like this:
select a.field1, b.field2, c.field3
from schema1.table1 a, schema2.table1 b, schema2.table2 c
I’m stuck on this as I’m not sure how to achieve this. If I try to print out the query resulting from a call like:
table1.objects.all(), it becomes a
table1. I probably want it to be a query on
schema1is also the name of the app that
table1 model is contained in.
One way I’ve achieved such a query to be generated was to modify each model’s
Meta.db_table to be prefixed with the name of the schema. This seems like a hack and I’m not even sure it works.
Is there a better way?
Also, I’m assuming that since this is all a single db, in the end I’ll have to put a single search_path option in the
default connection to include all schemas. Is that accurate?