Skip to content

Commit 5013411

Browse files
committed
Bug#25587256: NEWDD: FK: NEED TO SET PROPER REFERENCED COLUMN CASE
The problem was that the name of the columns referenced by a foreign key was not converted to lower case when they were stored in the data dictionary. This would have given a change of behavior when the data dictionary eventually is used for FK info in e.g. SHOW CREATE TABLE. On current trunk, InnoDB's data dictionary is still used, so this bug had no visible effects yet. This patch fixes the problem by converting referenced column names to lower case when they are stored in the data dictionary.
1 parent 22b28cb commit 5013411

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

mysql-test/r/dd_debug.result

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,26 @@ Warnings:
223223
Note 1051 Unknown table 'test.t1'
224224
DROP TABLE t2;
225225
DROP VIEW v1;
226+
#
227+
# Bug#25587256 NEWDD: FK: NEED TO SET PROPER REFERENCED COLUMN CASE
228+
#
229+
CREATE TABLE t1 (pk INT PRIMARY KEY);
230+
CREATE TABLE t2 (fk INT, FOREIGN KEY (FK) REFERENCES t1 (PK));
231+
SHOW CREATE TABLE t2;
232+
Table Create Table
233+
t2 CREATE TABLE `t2` (
234+
`fk` int(11) DEFAULT NULL,
235+
KEY `fk` (`fk`),
236+
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `t1` (`pk`)
237+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
238+
SET SESSION debug= '+d,skip_dd_table_access_check';
239+
SELECT foreign_key_column_usage.referenced_column_name
240+
FROM mysql.foreign_key_column_usage, mysql.foreign_keys, mysql.tables
241+
WHERE tables.name= 't2'
242+
AND tables.id = foreign_keys.table_id
243+
AND foreign_keys.id = foreign_key_column_usage.foreign_key_id;
244+
referenced_column_name
245+
pk
246+
SET SESSION debug= '-d,skip_dd_table_access_check';
247+
DROP TABLE t2, t1;
226248
###################################################################

mysql-test/t/dd_debug.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ DROP TABLE IF EXISTS t1;
284284
DROP TABLE t2;
285285
DROP VIEW v1;
286286

287+
--echo #
288+
--echo # Bug#25587256 NEWDD: FK: NEED TO SET PROPER REFERENCED COLUMN CASE
289+
--echo #
290+
291+
CREATE TABLE t1 (pk INT PRIMARY KEY);
292+
CREATE TABLE t2 (fk INT, FOREIGN KEY (FK) REFERENCES t1 (PK));
293+
294+
SHOW CREATE TABLE t2;
295+
SET SESSION debug= '+d,skip_dd_table_access_check';
296+
# Case should match SHOW CREATE TABLE output.
297+
SELECT foreign_key_column_usage.referenced_column_name
298+
FROM mysql.foreign_key_column_usage, mysql.foreign_keys, mysql.tables
299+
WHERE tables.name= 't2'
300+
AND tables.id = foreign_keys.table_id
301+
AND foreign_keys.id = foreign_key_column_usage.foreign_key_id;
302+
SET SESSION debug= '-d,skip_dd_table_access_check';
303+
304+
DROP TABLE t2, t1;
305+
287306
--echo ###################################################################
288307

289308
--disable_connect_log

sql/sql_table.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5919,7 +5919,12 @@ static bool prepare_foreign_key(THD *thd,
59195919
DBUG_RETURN(true);
59205920
}
59215921

5922-
fk_info->fk_key_part[column_nr]= fk_col->field_name;
5922+
// Always store column names in lower case.
5923+
char buff[NAME_LEN + 1];
5924+
my_stpncpy(buff, fk_col->field_name.str, NAME_LEN);
5925+
my_casedn_str(system_charset_info, buff);
5926+
fk_info->fk_key_part[column_nr].str= sql_strdup(buff);
5927+
fk_info->fk_key_part[column_nr].length= strlen(buff);
59235928
}
59245929
DBUG_RETURN(false);
59255930
}

0 commit comments

Comments
 (0)