﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22673	"inspectdb not support database schema on postgresql with name different of ""public"""	Fabio Caritas Barrionuevo da Luz <bnafta@…>	nobody	"I have a legacy database in PostgreSQL, which is still in production.
I want to migrate it to Django. 
I tried to do the reverse engineering of the database using inspectdb to generate models classes, however, this does not work.

'''The problem is Django not provides way to use inspectdb in a schema with name different of ""public""'''

If you do not know the concept of postgresql schema, please read:

http://www.postgresql.org/docs/9.3/static/ddl-schemas.html

To explain this, I created a database based on ""pulls"" app from the basic tutorial.

The backup of database I used is attached.


* It would facilitate integration with legacy database

I found these tickets dealing with similar things but different this

https://code.djangoproject.com/ticket/1051
https://code.djangoproject.com/ticket/6148


I set my settings like this:

{{{#!python


DATABASES = {
    # new blank database
    # this read/write into ""public"" postgresql schema
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'neweposse',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',      
    },
    # legacy database
    # the postgresql schema is named ""legacyschema1""
    'eposse': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'eposse',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',    
    }
}

}}}

'''in terminal:'''

{{{#!bash

(django1.7b4)oficina@oficina:~/schematest$ python manage.py inspectdb
...
from __future__ import unicode_literals

from django.db import models

(django1.7b4)oficina@oficina:~/schematest$ python manage.py inspectdb --database=eposse
...
from __future__ import unicode_literals

from django.db import models

}}}

'''Tests using dbshell, was recommended to me by Russell Keith-Magee this topic: https://groups.google.com/forum/#!topic/django-developers/lSHrDFZM4lQ'''

{{{#!bash

(django1.7b4)oficina@oficina:~/schematest$ python manage.py dbshell --database=eposse
psql (9.3.4)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type ""help"" for help.
 
eposse=# \dt
No relations found.

}}}

{{{#!bash

eposse=# \dn
     List of schemas
     Name      |  Owner   
---------------+----------
 legacyschema1 | postgres
 public        | postgres
(2 rows)
 
}}}

{{{#!bash

eposse=# \dt legacyschema1.*
             List of relations
    Schema     |  Name  | Type  |  Owner   
---------------+--------+-------+----------
 legacyschema1 | choice | table | postgres
 legacyschema1 | pull   | table | postgres
(2 rows)

}}}

{{{#!bash

eposse=# \d legacyschema1.*
                                       Table ""legacyschema1.choice""
   Column    |          Type          |                             Modifiers                             
-------------+------------------------+-------------------------------------------------------------------
 id          | integer                | not null default nextval('legacyschema1.choice_id_seq'::regclass)
 poll_id     | integer                | not null
 choice_text | character varying(200) | not null
 votes       | integer                | not null
Indexes:
    ""choice_pkey"" PRIMARY KEY, btree (id)
    ""choice_582e9e5a"" btree (poll_id)
Foreign-key constraints:
    ""choice_poll_id_3d0280c389b7efa_fk_pull_id"" FOREIGN KEY (poll_id) REFERENCES legacyschema1.pull(id) DEFERRABLE INITIALLY DEFERRED
 
Index ""legacyschema1.choice_582e9e5a""
 Column  |  Type   | Definition 
---------+---------+------------
 poll_id | integer | poll_id
btree, for table ""legacyschema1.choice""
 
    Sequence ""legacyschema1.choice_id_seq""
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | choice_id_seq
 last_value    | bigint  | 2
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | t
Owned by: legacyschema1.choice.id
 
Index ""legacyschema1.choice_pkey""
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table ""legacyschema1.choice""
 
                                      Table ""legacyschema1.pull""
  Column  |           Type           |                            Modifiers                            
----------+--------------------------+-----------------------------------------------------------------
 id       | integer                  | not null default nextval('legacyschema1.pull_id_seq'::regclass)
 question | character varying(200)   | not null
 pub_date | timestamp with time zone | not null
Indexes:
    ""pull_pkey"" PRIMARY KEY, btree (id)
Referenced by:
    TABLE ""legacyschema1.choice"" CONSTRAINT ""choice_poll_id_3d0280c389b7efa_fk_pull_id"" FOREIGN KEY (poll_id) REFERENCES legacyschema1.pull(id) DEFERRABLE INITIALLY DEFERRED
 
     Sequence ""legacyschema1.pull_id_seq""
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | pull_id_seq
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | t
Owned by: legacyschema1.pull.id
 
Index ""legacyschema1.pull_pkey""
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table ""legacyschema1.pull""

}}}"	New feature	new	Database layer (models, ORM)	dev	Normal		inspectdb, introspection, postgresql, schema	bnafta@… Ülgen Sarıkavak	Accepted	0	0	0	0	0	0
