Ticket #17574: 17574-2.diff

File 17574-2.diff, 3.8 KB (added by Claude Paroz, 12 years ago)

Updated after 09a9971

  • django/db/backends/postgresql_psycopg2/introspection.py

    diff --git a/django/db/backends/postgresql_psycopg2/introspection.py b/django/db/backends/postgresql_psycopg2/introspection.py
    index 5d30382..defceca 100644
    a b class DatabaseIntrospection(BaseDatabaseIntrospection):  
    6666            relations[row[0][0] - 1] = (row[1][0] - 1, row[2])
    6767        return relations
    6868
     69    def get_key_columns(self, cursor, table_name):
     70        key_columns = []
     71        cursor.execute("""
     72            SELECT kcu.column_name, ccu.table_name AS referenced_table, ccu.column_name AS referenced_column
     73            FROM information_schema.constraint_column_usage ccu
     74            LEFT JOIN information_schema.key_column_usage kcu
     75                ON ccu.constraint_catalog = kcu.constraint_catalog
     76                    AND ccu.constraint_schema = kcu.constraint_schema
     77                    AND ccu.constraint_name = kcu.constraint_name
     78            LEFT JOIN information_schema.table_constraints tc
     79                ON ccu.constraint_catalog = tc.constraint_catalog
     80                    AND ccu.constraint_schema = tc.constraint_schema
     81                    AND ccu.constraint_name = tc.constraint_name
     82            WHERE kcu.table_name = %s AND tc.constraint_type = 'FOREIGN KEY'""" , [table_name])
     83        key_columns.extend(cursor.fetchall())
     84        return key_columns
     85
    6986    def get_indexes(self, cursor, table_name):
    7087        # This query retrieves each index on the given table, including the
    7188        # first associated field name
  • tests/regressiontests/introspection/tests.py

    diff --git a/tests/regressiontests/introspection/tests.py b/tests/regressiontests/introspection/tests.py
    index a54e0c6..f1170b1 100644
    a b  
    1 from __future__ import absolute_import,unicode_literals
    2 
    3 from functools import update_wrapper
     1from __future__ import absolute_import, unicode_literals
    42
    53from django.db import connection
    64from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
    7 from django.utils import six
    85
    96from .models import Reporter, Article
    107
    11 #
    12 # The introspection module is optional, so methods tested here might raise
    13 # NotImplementedError. This is perfectly acceptable behavior for the backend
    14 # in question, but the tests need to handle this without failing. Ideally we'd
    15 # skip these tests, but until #4788 is done we'll just ignore them.
    16 #
    17 # The easiest way to accomplish this is to decorate every test case with a
    18 # wrapper that ignores the exception.
    19 #
    20 # The metaclass is just for fun.
    21 #
    22 
    23 def ignore_not_implemented(func):
    24     def _inner(*args, **kwargs):
    25         try:
    26             return func(*args, **kwargs)
    27         except NotImplementedError:
    28             return None
    29     update_wrapper(_inner, func)
    30     return _inner
    31 
    32 class IgnoreNotimplementedError(type):
    33     def __new__(cls, name, bases, attrs):
    34         for k,v in attrs.items():
    35             if k.startswith('test'):
    36                 attrs[k] = ignore_not_implemented(v)
    37         return type.__new__(cls, name, bases, attrs)
    38 
    39 class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase)):
     8
     9class IntrospectionTests(TestCase):
    4010
    4111    def test_table_names(self):
    4212        tl = connection.introspection.table_names()
    class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase)  
    12797            # That's {field_index: (field_index_other_table, other_table)}
    12898            self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
    12999
     100    @skipUnlessDBFeature('can_introspect_foreign_keys')
    130101    def test_get_key_columns(self):
    131102        cursor = connection.cursor()
    132103        key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)
Back to Top