Skip to content

Commit 19e3597

Browse files
author
Jan Lindström
committed
MDEV-9142 :Adding Constraint with no database reference
results in ERROR 1046 (3D000) at line 13: No database selected. Use database from create table to foreign key database if nothing else is given.
1 parent 0ea4c73 commit 19e3597

File tree

5 files changed

+113
-7
lines changed

5 files changed

+113
-7
lines changed

mysql-test/suite/innodb/r/innodb-fk.result

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,47 @@ Warning 150 Alter table `test`.`t2` with foreign key constraint failed. Referen
6868
Error 1005 Can't create table '#sql-temporary' (errno: 150)
6969
drop table t2;
7070
drop table t1;
71+
CREATE DATABASE kg_test1;
72+
CREATE DATABASE kg_test2;
73+
CREATE TABLE `kg_test1`.`group` (
74+
Id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
75+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
76+
CREATE TABLE `kg_test1`.`person` (
77+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
78+
`Name` VARCHAR(50) NOT NULL,
79+
PRIMARY KEY (`Id`),
80+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
81+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
82+
show create table `kg_test1`.`person`;
83+
Table Create Table
84+
person CREATE TABLE `person` (
85+
`Id` int(11) NOT NULL AUTO_INCREMENT,
86+
`Name` varchar(50) NOT NULL,
87+
PRIMARY KEY (`Id`),
88+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
89+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
90+
CREATE TABLE `kg_test2`.`person2` (
91+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
92+
`Name` VARCHAR(50) NOT NULL,
93+
PRIMARY KEY (`Id`),
94+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
95+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
96+
ERROR HY000: Can't create table 'kg_test2.person2' (errno: 150)
97+
CREATE TABLE `kg_test2`.`person2` (
98+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
99+
`Name` VARCHAR(50) NOT NULL,
100+
PRIMARY KEY (`Id`),
101+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `kg_test1`.`group` (`Id`)
102+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
103+
show create table `kg_test2`.`person2`;
104+
Table Create Table
105+
person2 CREATE TABLE `person2` (
106+
`Id` int(11) NOT NULL AUTO_INCREMENT,
107+
`Name` varchar(50) NOT NULL,
108+
PRIMARY KEY (`Id`),
109+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `kg_test1`.`group` (`Id`)
110+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
111+
SHOW WARNINGS;
112+
Level Code Message
113+
DROP DATABASE kg_test2;
114+
DROP DATABASE kg_test1;

mysql-test/suite/innodb/t/innodb-fk.test

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,44 @@ show warnings;
124124

125125
drop table t2;
126126
drop table t1;
127+
128+
#
129+
# MDEV-9142 :Adding Constraint with no database reference
130+
# results in ERROR 1046 (3D000) at line 13: No database selected
131+
#
132+
CREATE DATABASE kg_test1;
133+
CREATE DATABASE kg_test2;
134+
135+
CREATE TABLE `kg_test1`.`group` (
136+
Id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
137+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
138+
139+
CREATE TABLE `kg_test1`.`person` (
140+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
141+
`Name` VARCHAR(50) NOT NULL,
142+
PRIMARY KEY (`Id`),
143+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
144+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
145+
146+
show create table `kg_test1`.`person`;
147+
148+
--error 1005
149+
CREATE TABLE `kg_test2`.`person2` (
150+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
151+
`Name` VARCHAR(50) NOT NULL,
152+
PRIMARY KEY (`Id`),
153+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
154+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
155+
156+
CREATE TABLE `kg_test2`.`person2` (
157+
`Id` INT(11) NOT NULL AUTO_INCREMENT,
158+
`Name` VARCHAR(50) NOT NULL,
159+
PRIMARY KEY (`Id`),
160+
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `kg_test1`.`group` (`Id`)
161+
) ENGINE=INNODB DEFAULT CHARSET=utf8;
162+
163+
show create table `kg_test2`.`person2`;
164+
165+
SHOW WARNINGS;
166+
DROP DATABASE kg_test2;
167+
DROP DATABASE kg_test1;

sql/sql_parse.cc

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5395,6 +5395,7 @@ bool check_global_access(THD *thd, ulong want_access, bool no_errors)
53955395
temporary table flag)
53965396
@param alter_info [in] Initial list of columns and indexes for the
53975397
table to be created
5398+
@param create_db [in] Database of the created table
53985399
53995400
@retval
54005401
false ok.
@@ -5403,7 +5404,8 @@ bool check_global_access(THD *thd, ulong want_access, bool no_errors)
54035404
*/
54045405
bool check_fk_parent_table_access(THD *thd,
54055406
HA_CREATE_INFO *create_info,
5406-
Alter_info *alter_info)
5407+
Alter_info *alter_info,
5408+
const char* create_db)
54075409
{
54085410
Key *key;
54095411
List_iterator<Key> key_iterator(alter_info->key_list);
@@ -5443,10 +5445,28 @@ bool check_fk_parent_table_access(THD *thd,
54435445
return true;
54445446
}
54455447
}
5446-
else if (thd->lex->copy_db_to(&db_name.str, &db_name.length))
5447-
return true;
54485448
else
5449-
is_qualified_table_name= false;
5449+
{
5450+
if (!thd->db)
5451+
{
5452+
db_name.str= (char *) thd->memdup(create_db, strlen(create_db)+1);
5453+
db_name.length= strlen(create_db);
5454+
is_qualified_table_name= true;
5455+
5456+
if(create_db && check_db_name(&db_name))
5457+
{
5458+
my_error(ER_WRONG_DB_NAME, MYF(0), db_name.str);
5459+
return true;
5460+
}
5461+
}
5462+
else
5463+
{
5464+
if (thd->lex->copy_db_to(&db_name.str, &db_name.length))
5465+
return true;
5466+
else
5467+
is_qualified_table_name= false;
5468+
}
5469+
}
54505470

54515471
// if lower_case_table_names is set then convert tablename to lower case.
54525472
if (lower_case_table_names)
@@ -7462,7 +7482,7 @@ bool create_table_precheck(THD *thd, TABLE_LIST *tables,
74627482
goto err;
74637483
}
74647484

7465-
if (check_fk_parent_table_access(thd, &lex->create_info, &lex->alter_info))
7485+
if (check_fk_parent_table_access(thd, &lex->create_info, &lex->alter_info, create_table->db))
74667486
goto err;
74677487

74687488
error= FALSE;

sql/sql_parse.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ bool create_table_precheck(THD *thd, TABLE_LIST *tables,
4747
TABLE_LIST *create_table);
4848
bool check_fk_parent_table_access(THD *thd,
4949
HA_CREATE_INFO *create_info,
50-
Alter_info *alter_info);
50+
Alter_info *alter_info,
51+
const char* create_db);
5152

5253
bool parse_sql(THD *thd,
5354
Parser_state *parser_state,

sql/sql_table.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6305,7 +6305,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
63056305
till this point for the alter operation.
63066306
*/
63076307
if ((alter_info->flags & ALTER_FOREIGN_KEY) &&
6308-
check_fk_parent_table_access(thd, create_info, alter_info))
6308+
check_fk_parent_table_access(thd, create_info, alter_info, new_db))
63096309
goto err;
63106310

63116311
/*

0 commit comments

Comments
 (0)