Opened 8 years ago

Closed 8 years ago

#27908 closed Bug (duplicate)

Cannot use 'schema.table' with 'inspectdb'

Reported by: Jaap Vermeulen Owned by: nobody
Component: Core (Management commands) Version: 1.10
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Tim Graham, 8 years ago

Is this a duplicate of #22673?

comment:2 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top