[Django] #27908: Cannot use 'schema.table' with 'inspectdb'

14 views
Skip to first unread message

Django

unread,
Mar 7, 2017, 4:27:31 PM3/7/17
to django-...@googlegroups.com
#27908: Cannot use 'schema.table' with 'inspectdb'
-------------------------------------+-------------------------------------
Reporter: Jaap | Owner: nobody
Vermeulen |
Type: Bug | Status: new
Component: Core | Version: 1.10
(Management commands) |
Severity: Normal | Keywords: inspectdb
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I tried inspecting a table in a different schema with Django 1.10.6 and
python 3.4.5 and postgres 9.2.18 on RHEL7.
Looks like the 'inspectdb' doesn't really handle that. You can get past
the first error (table not found) with:
python3 manage.py inspectdb '"schema"."table"'
by tricking the quoting code in quote_name() in
db/backends/postgresql/operations.py, but it still goes wrong for the code
that figures out NULLable. Here are 2 snippets of code that will at least
make it possible to inspect tables in a different schema (I didn't peel a
git clone just for this; use it if you think it's useful):

db/backends/postgresql/operations.py:

{{{
def quote_name(self, name):
if name.startswith('"') and name.endswith('"'):
return name # Quoting once is enough.
# Quote db and schema names separately
parts = name.split('.')
quoted_name = ""
seperator = ''
for part in name.split('.'):
quoted_name += seperator
quoted_name += '"%s"' % part
seperator = '.'

return quoted_name

def unquote_name(self, name):
if name.startswith('"') and name.endswith('"'):
name = name[1:-1]
# Quote only name
parts = name.split('.')
return "%s" % parts[-1]
}}}

db/backends/postgresql/introspection.py:

{{{
def get_table_description(...):
...
cursor.execute("""
SELECT column_name, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = %s""",
[self.connection.ops.unquote_name(table_name)])
...

}}}

Note that the code is crummy at best. Really, to deal with schemas you
should look at both the table_name and the schema_name as needed for the
information_schema.columns. But as long as your table name is unique,
this at least works for now.

--
Ticket URL: <https://code.djangoproject.com/ticket/27908>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 7, 2017, 6:47:35 PM3/7/17
to django-...@googlegroups.com
#27908: Cannot use 'schema.table' with 'inspectdb'
-------------------------------------+-------------------------------------
Reporter: Jaap Vermeulen | Owner: nobody
Type: Bug | Status: new
Component: Core (Management | Version: 1.10
commands) |
Severity: Normal | Resolution:
Keywords: inspectdb | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Is this a duplicate of #22673?

--
Ticket URL: <https://code.djangoproject.com/ticket/27908#comment:1>

Django

unread,
Mar 8, 2017, 8:39:21 PM3/8/17
to django-...@googlegroups.com
#27908: Cannot use 'schema.table' with 'inspectdb'
-------------------------------------+-------------------------------------
Reporter: Jaap Vermeulen | Owner: nobody
Type: Bug | Status: closed

Component: Core (Management | Version: 1.10
commands) |
Severity: Normal | Resolution: duplicate

Keywords: inspectdb | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


--
Ticket URL: <https://code.djangoproject.com/ticket/27908#comment:2>

Reply all
Reply to author
Forward
0 new messages