Opened 17 years ago

Closed 11 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

Description

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/introspection.py
===================================================================
--- django/db/backends/postgresql_psycopg2/introspection.py	(revision 7196)
+++ django/db/backends/postgresql_psycopg2/introspection.py	(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 by Malcolm Tredinnick, 17 years ago

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 by Michael van der Westhuizen <r1mikey@…>, 17 years ago

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 by Jacob, 17 years ago

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 by Malcolm Tredinnick, 14 years ago

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 by Adam Vandenberg, 14 years ago

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: http://code.djangoproject.com/ticket/14098

comment:6 by Gabriel Hurley, 14 years ago

Component: django-admin.py inspectdbCore (Management commands)

comment:7 by Julien Phalip, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:8 by Ramiro Morales, 13 years ago

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

comment:9 by Claude Paroz <claude@…>, 11 years ago

In 2af58a2cef8b0d30c18f6d0d106cb0e093c673ab:

Made sqlite introspection also show views like other backends

Refs #6730.

comment:10 by Claude Paroz, 11 years ago

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