#25476: bug in array handling hit in get_constraints() for psycopg2?
-------------------------------------+-------------------------------------
     Reporter:  heasus               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  psycopg2 fetchall    |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by gagangupt16):

 Facing similar issue while using inspectdb.

 Environment:
 Python: 3.4.3
 Django: 1.9
 PostgreSQL: 9.3.10
 psycopg2: 2.6.1


 {{{
 Traceback (most recent call last):
   File "manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/__init__.py", line 350, in
 execute_from_command_line
     utility.execute()
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/__init__.py", line 342, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/base.py", line 348, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/base.py", line 399, in execute
     output = self.handle(*args, **options)
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/commands/inspectdb.py", line 25, in handle
     for line in self.handle_inspection(options):
   File "/usr/local/lib/python3.4/dist-
 packages/django/core/management/commands/inspectdb.py", line 70, in
 handle_inspection
     constraints = connection.introspection.get_constraints(cursor,
 table_name)
   File "/usr/local/lib/python3.4/dist-
 packages/django/db/backends/postgresql/introspection.py", line 171, in
 get_constraints
     "foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() ==
 "foreign key" else None,
 IndexError: list index out of range
 }}}


 In this query,

 {{{
 cursor.execute("""
             SELECT
                 kc.constraint_name,
                 kc.column_name,
                 c.constraint_type,
                 array(SELECT table_name::text || '.' || column_name::text
                       FROM information_schema.constraint_column_usage
                       WHERE constraint_name = kc.constraint_name)
             FROM information_schema.key_column_usage AS kc
             JOIN information_schema.table_constraints AS c ON
                 kc.table_schema = c.table_schema AND
                 kc.table_name = c.table_name AND
                 kc.constraint_name = c.constraint_name
             WHERE
                 kc.table_schema = %s AND
                 kc.table_name = %s
             ORDER BY kc.ordinal_position ASC
         """, ["public", table_name])
 }}}



 The subquery


 {{{
 "SELECT table_name::text || '.' || column_name::text
                       FROM information_schema.constraint_column_usage
                       WHERE constraint_name = kc.constraint_name"
 }}}

 is returning empty array.

 Hence, Index out of bound exception.
 However on running the same query on postgresql console and replacing with
 appropriate values. It was not returning array.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.39a822a285d9468a249699bf6a72f323%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to