Ticket #18082: 18082.diff
File 18082.diff, 4.0 KB (added by , 13 years ago) |
---|
-
django/db/backends/oracle/introspection.py
diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index b8a8b2e..8db3e7d 100644
a b class DatabaseIntrospection(BaseDatabaseIntrospection): 72 72 FROM user_constraints, USER_CONS_COLUMNS ca, USER_CONS_COLUMNS cb, 73 73 user_tab_cols ta, user_tab_cols tb 74 74 WHERE user_constraints.table_name = %s AND 75 ta.table_name = %sAND75 ta.table_name = user_constraints.table_name AND 76 76 ta.column_name = ca.column_name AND 77 ca.table_name = %sAND77 ca.table_name = ta.table_name AND 78 78 user_constraints.constraint_name = ca.constraint_name AND 79 79 user_constraints.r_constraint_name = cb.constraint_name AND 80 80 cb.table_name = tb.table_name AND 81 81 cb.column_name = tb.column_name AND 82 ca.position = cb.position""", [table_name , table_name, table_name])82 ca.position = cb.position""", [table_name]) 83 83 84 84 relations = {} 85 85 for row in cursor.fetchall(): … … class DatabaseIntrospection(BaseDatabaseIntrospection): 93 93 {'primary_key': boolean representing whether it's the primary key, 94 94 'unique': boolean representing whether it's a unique index} 95 95 """ 96 # This query retrieves each index on the given table, including the 97 # first associated field name 98 # "We were in the nick of time; you were in great peril!" 96 # A column can be a part of multiple indexes. Our introspection 97 # logic ends at "if it is part of any unique index then it is unique". 98 # This can naturally be wrong - maybe it is part of a multicolumn 99 # unique index. 100 # 101 # The below SQL uses GROUP BY plus MAX to check if the index is part 102 # of any UNIQUE index. 99 103 sql = """\ 100 SELECT LOWER(all_tab_cols.column_name) AS column_name, 101 CASE user_constraints.constraint_type 102 WHEN 'P' THEN 1 ELSE 0 103 END AS is_primary_key, 104 CASE user_indexes.uniqueness 105 WHEN 'UNIQUE' THEN 1 ELSE 0 106 END AS is_unique 107 FROM all_tab_cols, user_cons_columns, user_constraints, user_ind_columns, user_indexes 108 WHERE all_tab_cols.column_name = user_cons_columns.column_name (+) 109 AND all_tab_cols.table_name = user_cons_columns.table_name (+) 110 AND user_cons_columns.constraint_name = user_constraints.constraint_name (+) 111 AND user_constraints.constraint_type (+) = 'P' 112 AND user_ind_columns.column_name (+) = all_tab_cols.column_name 113 AND user_ind_columns.table_name (+) = all_tab_cols.table_name 114 AND user_indexes.uniqueness (+) = 'UNIQUE' 115 AND user_indexes.index_name (+) = user_ind_columns.index_name 116 AND all_tab_cols.table_name = UPPER(%s) 104 SELECT column_name, MAX(is_primary_key) as is_primary_key, 105 MAX(is_unique) as is_unique 106 FROM ( 107 SELECT LOWER(user_ind_columns.column_name) AS column_name, 108 CASE user_constraints.constraint_type 109 WHEN 'P' THEN 1 ELSE 0 110 END AS is_primary_key, 111 CASE user_indexes.uniqueness 112 WHEN 'UNIQUE' THEN 1 ELSE 0 113 END AS is_unique 114 FROM user_constraints, user_ind_columns, user_indexes 115 WHERE user_constraints.constraint_type (+) = 'P' 116 AND user_constraints.index_name (+) = user_ind_columns.index_name 117 AND user_indexes.uniqueness (+) = 'UNIQUE' 118 AND user_indexes.index_name (+) = user_ind_columns.index_name 119 AND user_ind_columns.table_name = UPPER(%s) 120 AND user_ind_columns.column_position = 1 121 ) 122 GROUP BY column_name 117 123 """ 118 124 cursor.execute(sql, [table_name]) 119 125 indexes = {} 120 126 for row in cursor.fetchall(): 121 indexes[row[0]] = {'primary_key': row[1], 'unique': row[2]} 127 indexes[row[0]] = {'primary_key': bool(row[1]), 128 'unique': bool(row[2])} 122 129 return indexes