Ticket #17574: 17574-2.diff
File 17574-2.diff, 3.8 KB (added by , 12 years ago) |
---|
-
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): 66 66 relations[row[0][0] - 1] = (row[1][0] - 1, row[2]) 67 67 return relations 68 68 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 69 86 def get_indexes(self, cursor, table_name): 70 87 # This query retrieves each index on the given table, including the 71 88 # 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 1 from __future__ import absolute_import, unicode_literals 4 2 5 3 from django.db import connection 6 4 from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature 7 from django.utils import six8 5 9 6 from .models import Reporter, Article 10 7 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 9 class IntrospectionTests(TestCase): 40 10 41 11 def test_table_names(self): 42 12 tl = connection.introspection.table_names() … … class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase) 127 97 # That's {field_index: (field_index_other_table, other_table)} 128 98 self.assertEqual(relations, {3: (0, Reporter._meta.db_table)}) 129 99 100 @skipUnlessDBFeature('can_introspect_foreign_keys') 130 101 def test_get_key_columns(self): 131 102 cursor = connection.cursor() 132 103 key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)