6

I'm trying to develop a django interface for an existing postgresql db, the db makes use of various schemas, looking at the literature the following example should work, but it only returns the schema defined in the default database when I run python manaage.py inspectdb. Also when this works, how do I define which schema to use when defining the django model?

DATABASES = {

'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'OPTIONS' : {
            'options': '-c search_path=public'
        },
    'NAME': 'gygaia',
    'USER':'postgres',
    'PASSWORD':'abc',
    'HOST':'localhost',
    'PORT':'5432',
},

'samples': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'OPTIONS' : {
            'options': '-c search_path=samples'
        },
    'NAME': 'gygaia',
    'USER':'postgres',
    'PASSWORD':'abc',
    'HOST':'localhost',
    'PORT':'5432',
},
    'excavation': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'OPTIONS' : {
                'options': '-c search_path=excavation'
            },
        'NAME': 'gygaia',
        'USER':'postgres',
        'PASSWORD':'abc',
        'HOST':'localhost',
        'PORT':'5432',
    },
}
Spatial Digger
  • 207
  • 1
  • 4
  • 8

2 Answers2

6

Here is a super easy project example with 2 apps and 3 schemes that cross each other with ForeignKey

https://github.com/mullerivan/DjangomultipleSchema
The idea is to define each model with the schema and table that need to use

class Meta:
    db_table = u'"app1\".\"on_app_one"'
mullerivan
  • 169
  • 1
  • 3
-1

Update OPTIONS as

'OPTIONS' : {
            'options': '-c search_path=\"samples\"'
        },

I just tried using that syntax and it worked pretty well and no other syntax specified in any of the StackExchange answers worked for me. Upon my analysis, it so happens to be the reason that for PostgreSQL version >= 9.2, we can specify schema name in Django app settings between double quotations so that the Django app doesn't see the schema name too as some normal entity.