Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17574 -- Implemented missing get_key_columns in PostgreSQL bac…

…kend
  • Loading branch information...
commit 0171ba65dbbff377282c03b86c83036168c84b22 1 parent 223fc8e
Claude Paroz authored January 12, 2013
17  django/db/backends/postgresql_psycopg2/introspection.py
@@ -66,6 +66,23 @@ def get_relations(self, cursor, table_name):
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
36  tests/regressiontests/introspection/tests.py
... ...
@@ -1,10 +1,8 @@
1 1
 from __future__ import absolute_import, unicode_literals
2 2
 
3  
-from functools import update_wrapper
4  
-
5 3
 from django.db import connection
6 4
 from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
7  
-from django.utils import six, unittest
  5
+from django.utils import unittest
8 6
 
9 7
 from .models import Reporter, Article
10 8
 
@@ -14,36 +12,7 @@
14 12
     expectedFailureOnOracle = lambda f: f
15 13
 
16 14
 
17  
-# The introspection module is optional, so methods tested here might raise
18  
-# NotImplementedError. This is perfectly acceptable behavior for the backend
19  
-# in question, but the tests need to handle this without failing. Ideally we'd
20  
-# skip these tests, but until #4788 is done we'll just ignore them.
21  
-#
22  
-# The easiest way to accomplish this is to decorate every test case with a
23  
-# wrapper that ignores the exception.
24  
-#
25  
-# The metaclass is just for fun.
26  
-
27  
-
28  
-def ignore_not_implemented(func):
29  
-    def _inner(*args, **kwargs):
30  
-        try:
31  
-            return func(*args, **kwargs)
32  
-        except NotImplementedError:
33  
-            return None
34  
-    update_wrapper(_inner, func)
35  
-    return _inner
36  
-
37  
-
38  
-class IgnoreNotimplementedError(type):
39  
-    def __new__(cls, name, bases, attrs):
40  
-        for k, v in attrs.items():
41  
-            if k.startswith('test'):
42  
-                attrs[k] = ignore_not_implemented(v)
43  
-        return type.__new__(cls, name, bases, attrs)
44  
-
45  
-
46  
-class IntrospectionTests(six.with_metaclass(IgnoreNotimplementedError, TestCase)):
  15
+class IntrospectionTests(TestCase):
47 16
     def test_table_names(self):
48 17
         tl = connection.introspection.table_names()
49 18
         self.assertEqual(tl, sorted(tl))
@@ -139,6 +108,7 @@ def test_get_relations(self):
139 108
             # That's {field_index: (field_index_other_table, other_table)}
140 109
             self.assertEqual(relations, {3: (0, Reporter._meta.db_table)})
141 110
 
  111
+    @skipUnlessDBFeature('can_introspect_foreign_keys')
142 112
     def test_get_key_columns(self):
143 113
         cursor = connection.cursor()
144 114
         key_columns = connection.introspection.get_key_columns(cursor, Article._meta.db_table)

0 notes on commit 0171ba6

Please sign in to comment.
Something went wrong with that request. Please try again.