Opened 13 years ago

Closed 8 years ago

#6730 closed Cleanup/optimization (fixed)

postgresql_psycopg2 introspection includes views in generated models

Reported by: Michael van der Westhuizen <r1mikey@…> Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


It's a little unexpected that inspectdb for psycopg2 includes views in its output. This is also inconsistent with the Oracle backend. The following patch corrects this behavior and gives me the results I expect:

Index: django/db/backends/postgresql_psycopg2/
--- django/db/backends/postgresql_psycopg2/	(revision 7196)
+++ django/db/backends/postgresql_psycopg2/	(working copy)
@@ -8,7 +8,7 @@
         SELECT c.relname
         FROM pg_catalog.pg_class c
         LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
-        WHERE c.relkind IN ('r', 'v', '')
+        WHERE c.relkind = 'r'
             AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
             AND pg_catalog.pg_table_is_visible(c.oid)""")
     return [row[0] for row in cursor.fetchall()]

Change History (10)

comment:1 Changed 13 years ago by Malcolm Tredinnick

It's not clear that this is incorrect behaviour. Creating models over views isn't a bad thing (it works, providing you don't try to save()). Anybody using inspectdb is expected to examine the output and only use the models they need in any case.

I think this is probably better done as a documentation clarification than removing functionality.

comment:2 Changed 13 years ago by Michael van der Westhuizen <r1mikey@…>

Fair enough, but psycopg and psycopg2 backends are the only backends in trunk which have this behavior - sqlite3, oracle and mysql all only return tables.

comment:3 Changed 13 years ago by Jacob

Has patch: set
Triage Stage: UnreviewedDesign decision needed

Yeah - we to be consistent here. Not sure which is correct, but being inconsistent is silly.

comment:4 Changed 11 years ago by Malcolm Tredinnick

Patch needs improvement: set
Triage Stage: Design decision neededAccepted

We should process views and make them managed models (Malcolm + Russ decision). Those backends that don't do it at the moment can be done piecemeal -- this isn't an all-or-nothing change before it goes in. But Meta.managed = True would be good here.

comment:5 Changed 11 years ago by Adam Vandenberg

MySQL, at least version 5.1.x w/ Django 1.3.x, also returns views during inspectdb (which is the behavior I want). Though because views can be ill-defined in MySQL (refer to tables/columns that no longer exist), I filed this ticket:

comment:6 Changed 10 years ago by Gabriel Hurley

Component: inspectdbCore (Management commands)

comment:7 Changed 10 years ago by Julien Phalip

Severity: Normal
Type: Cleanup/optimization

comment:8 Changed 10 years ago by Ramiro Morales

Easy pickings: unset
Keywords: inspectdb added
UI/UX: unset

comment:9 Changed 8 years ago by Claude Paroz <claude@…>

In 2af58a2cef8b0d30c18f6d0d106cb0e093c673ab:

Made sqlite introspection also show views like other backends

Refs #6730.

comment:10 Changed 8 years ago by Claude Paroz

Resolution: fixed
Status: newclosed

I think only Oracle doesn't return views now. I've created ticket #21272 for that issue.

#18782 is related, where I suggested a patch to return more info from get_table_list (namely if table is real table or a view).
As inspectdb now always adds managed=False, I think that this ticket is obsolete.

Note: See TracTickets for help on using tickets.
Back to Top