mirror of https://github.com/django/django.git
Simplified introspection of constraints on MySQL.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
parent
694deff82f
commit
c60b9e6640
|
@ -211,20 +211,26 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
||||||
# Get the actual constraint names and columns
|
# Get the actual constraint names and columns
|
||||||
name_query = """
|
name_query = """
|
||||||
SELECT kc.`constraint_name`, kc.`column_name`,
|
SELECT kc.`constraint_name`, kc.`column_name`,
|
||||||
kc.`referenced_table_name`, kc.`referenced_column_name`
|
kc.`referenced_table_name`, kc.`referenced_column_name`,
|
||||||
FROM information_schema.key_column_usage AS kc
|
c.`constraint_type`
|
||||||
|
FROM
|
||||||
|
information_schema.key_column_usage AS kc,
|
||||||
|
information_schema.table_constraints AS c
|
||||||
WHERE
|
WHERE
|
||||||
kc.table_schema = DATABASE() AND
|
kc.table_schema = DATABASE() AND
|
||||||
|
c.table_schema = kc.table_schema AND
|
||||||
|
c.constraint_name = kc.constraint_name AND
|
||||||
|
c.constraint_type != 'CHECK' AND
|
||||||
kc.table_name = %s
|
kc.table_name = %s
|
||||||
ORDER BY kc.`ordinal_position`
|
ORDER BY kc.`ordinal_position`
|
||||||
"""
|
"""
|
||||||
cursor.execute(name_query, [table_name])
|
cursor.execute(name_query, [table_name])
|
||||||
for constraint, column, ref_table, ref_column in cursor.fetchall():
|
for constraint, column, ref_table, ref_column, kind in cursor.fetchall():
|
||||||
if constraint not in constraints:
|
if constraint not in constraints:
|
||||||
constraints[constraint] = {
|
constraints[constraint] = {
|
||||||
'columns': OrderedSet(),
|
'columns': OrderedSet(),
|
||||||
'primary_key': False,
|
'primary_key': kind == 'PRIMARY KEY',
|
||||||
'unique': False,
|
'unique': kind in {'PRIMARY KEY', 'UNIQUE'},
|
||||||
'index': False,
|
'index': False,
|
||||||
'check': False,
|
'check': False,
|
||||||
'foreign_key': (ref_table, ref_column) if ref_column else None,
|
'foreign_key': (ref_table, ref_column) if ref_column else None,
|
||||||
|
@ -232,21 +238,6 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
||||||
if self.connection.features.supports_index_column_ordering:
|
if self.connection.features.supports_index_column_ordering:
|
||||||
constraints[constraint]['orders'] = []
|
constraints[constraint]['orders'] = []
|
||||||
constraints[constraint]['columns'].add(column)
|
constraints[constraint]['columns'].add(column)
|
||||||
# Now get the constraint types
|
|
||||||
type_query = """
|
|
||||||
SELECT c.constraint_name, c.constraint_type
|
|
||||||
FROM information_schema.table_constraints AS c
|
|
||||||
WHERE
|
|
||||||
c.table_schema = DATABASE() AND
|
|
||||||
c.table_name = %s
|
|
||||||
"""
|
|
||||||
cursor.execute(type_query, [table_name])
|
|
||||||
for constraint, kind in cursor.fetchall():
|
|
||||||
if kind.lower() == "primary key":
|
|
||||||
constraints[constraint]['primary_key'] = True
|
|
||||||
constraints[constraint]['unique'] = True
|
|
||||||
elif kind.lower() == "unique":
|
|
||||||
constraints[constraint]['unique'] = True
|
|
||||||
# Add check constraints.
|
# Add check constraints.
|
||||||
if self.connection.features.can_introspect_check_constraints:
|
if self.connection.features.can_introspect_check_constraints:
|
||||||
unnamed_constraints_index = 0
|
unnamed_constraints_index = 0
|
||||||
|
|
Loading…
Reference in New Issue