Fixed #27324 -- Simplified DatabaseIntrospection.get_constraints() on Oracle.
This commit is contained in:
parent
424187ec4b
commit
5a772a0b7b
|
@ -158,7 +158,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
||||||
Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
|
Retrieves any constraints or keys (unique, pk, fk, check, index) across one or more columns.
|
||||||
"""
|
"""
|
||||||
constraints = {}
|
constraints = {}
|
||||||
# Loop over the constraints, getting PKs and uniques
|
# Loop over the constraints, getting PKs, uniques, and checks
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
SELECT
|
SELECT
|
||||||
user_constraints.constraint_name,
|
user_constraints.constraint_name,
|
||||||
|
@ -167,29 +167,34 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
||||||
WHEN 'P' THEN 1
|
WHEN 'P' THEN 1
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END AS is_primary_key,
|
END AS is_primary_key,
|
||||||
CASE user_indexes.uniqueness
|
CASE
|
||||||
WHEN 'UNIQUE' THEN 1
|
WHEN EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM user_indexes
|
||||||
|
WHERE user_indexes.index_name = user_constraints.index_name
|
||||||
|
AND user_indexes.uniqueness = 'UNIQUE'
|
||||||
|
)
|
||||||
|
THEN 1
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END AS is_unique,
|
END AS is_unique,
|
||||||
CASE user_constraints.constraint_type
|
CASE user_constraints.constraint_type
|
||||||
WHEN 'C' THEN 1
|
WHEN 'C' THEN 1
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END AS is_check_constraint
|
END AS is_check_constraint,
|
||||||
|
CASE
|
||||||
|
WHEN user_constraints.constraint_type IN ('P', 'U') THEN 1
|
||||||
|
ELSE 0
|
||||||
|
END AS has_index
|
||||||
FROM
|
FROM
|
||||||
user_constraints
|
user_constraints
|
||||||
INNER JOIN
|
|
||||||
user_indexes ON user_indexes.index_name = user_constraints.index_name
|
|
||||||
LEFT OUTER JOIN
|
LEFT OUTER JOIN
|
||||||
user_cons_columns cols ON user_constraints.constraint_name = cols.constraint_name
|
user_cons_columns cols ON user_constraints.constraint_name = cols.constraint_name
|
||||||
WHERE
|
WHERE
|
||||||
(
|
user_constraints.constraint_type = ANY('P', 'U', 'C')
|
||||||
user_constraints.constraint_type = 'P' OR
|
|
||||||
user_constraints.constraint_type = 'U'
|
|
||||||
)
|
|
||||||
AND user_constraints.table_name = UPPER(%s)
|
AND user_constraints.table_name = UPPER(%s)
|
||||||
ORDER BY cols.position
|
ORDER BY cols.position
|
||||||
""", [table_name])
|
""", [table_name])
|
||||||
for constraint, column, pk, unique, check in cursor.fetchall():
|
for constraint, column, pk, unique, check, index in cursor.fetchall():
|
||||||
# If we're the first column, make the record
|
# If we're the first column, make the record
|
||||||
if constraint not in constraints:
|
if constraint not in constraints:
|
||||||
constraints[constraint] = {
|
constraints[constraint] = {
|
||||||
|
@ -198,34 +203,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
||||||
"unique": unique,
|
"unique": unique,
|
||||||
"foreign_key": None,
|
"foreign_key": None,
|
||||||
"check": check,
|
"check": check,
|
||||||
"index": True, # All P and U come with index, see inner join above
|
"index": index, # All P and U come with index
|
||||||
}
|
|
||||||
# Record the details
|
|
||||||
constraints[constraint]['columns'].append(column)
|
|
||||||
# Check constraints
|
|
||||||
cursor.execute("""
|
|
||||||
SELECT
|
|
||||||
cons.constraint_name,
|
|
||||||
LOWER(cols.column_name) AS column_name
|
|
||||||
FROM
|
|
||||||
user_constraints cons
|
|
||||||
LEFT OUTER JOIN
|
|
||||||
user_cons_columns cols ON cons.constraint_name = cols.constraint_name
|
|
||||||
WHERE
|
|
||||||
cons.constraint_type = 'C' AND
|
|
||||||
cons.table_name = UPPER(%s)
|
|
||||||
ORDER BY cols.position
|
|
||||||
""", [table_name])
|
|
||||||
for constraint, column in cursor.fetchall():
|
|
||||||
# If we're the first column, make the record
|
|
||||||
if constraint not in constraints:
|
|
||||||
constraints[constraint] = {
|
|
||||||
"columns": [],
|
|
||||||
"primary_key": False,
|
|
||||||
"unique": False,
|
|
||||||
"foreign_key": None,
|
|
||||||
"check": True,
|
|
||||||
"index": False,
|
|
||||||
}
|
}
|
||||||
# Record the details
|
# Record the details
|
||||||
constraints[constraint]['columns'].append(column)
|
constraints[constraint]['columns'].append(column)
|
||||||
|
|
Loading…
Reference in New Issue