Opened 7 years ago

Closed 20 months 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: master
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 Changed 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 7 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 7 years ago by jacob

  • Has patch set
  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:4 Changed 5 years ago by mtredinnick

  • Patch needs improvement set
  • Triage Stage changed from Design decision needed to Accepted

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 5 years ago by adamv

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 Changed 4 years ago by gabrielhurley

  • Component changed from django-admin.py inspectdb to Core (Management commands)

comment:7 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Cleanup/optimization

comment:8 Changed 4 years ago by ramiro

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

comment:9 Changed 20 months ago by Claude Paroz <claude@…>

In 2af58a2cef8b0d30c18f6d0d106cb0e093c673ab:

Made sqlite introspection also show views like other backends

Refs #6730.

comment:10 Changed 20 months ago by claudep

  • Resolution set to fixed
  • Status changed from new to closed

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