summaryrefslogtreecommitdiff
path: root/sql/sql_table.cc
diff options
context:
space:
mode:
authorJan Lindström <jan.lindstrom@mariadb.com>2022-08-05 13:31:10 +0300
committerJan Lindström <jan.lindstrom@mariadb.com>2022-08-05 13:31:10 +0300
commitba80fb383c7172eaa77375d2945fad77829df9c6 (patch)
tree59dee1332b07d0d96a665ab85395680fec73365b /sql/sql_table.cc
parent6adfce9c8d2a63a259dd0504600271498dcda228 (diff)
downloadmariadb-git-bb-10.4-MDEV-22230.tar.gz
MDEV-22230 : Unexpected ER_ERROR_ON_RENAME upon DROP non-existing FOREIGN KEY with ALGORITHM=COPYbb-10.4-MDEV-22230
If we are dropping a foreign key and expect it to exists, we should check does it exists and if not report a error in early stage i.e. in mysql_prepare_alter_table.
Diffstat (limited to 'sql/sql_table.cc')
-rw-r--r--sql/sql_table.cc22
1 files changed, 22 insertions, 0 deletions
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 2e581134507..9eb0ffc9c6c 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -8891,10 +8891,32 @@ mysql_prepare_alter_table(THD *thd, TABLE *table,
alter_info->drop_list.head()->name);
goto err;
case Alter_drop::FOREIGN_KEY:
+ /* If we are dropping a foreign key and expect it to exists,
+ we can now check does it exists and if not report a error. */
+ if (!drop->drop_if_exists)
+ {
+ List <FOREIGN_KEY_INFO> fk_child_key_list;
+ FOREIGN_KEY_INFO *f_key;
+ table->file->get_foreign_key_list(thd, &fk_child_key_list);
+ List_iterator<FOREIGN_KEY_INFO> fk_key_it(fk_child_key_list);
+ while ((f_key= fk_key_it++))
+ {
+ if (my_strcasecmp(system_charset_info, f_key->foreign_id->str,
+ drop->name) == 0)
+ break;
+ }
+ if (!f_key)
+ {
+ my_error(ER_CANT_DROP_FIELD_OR_KEY, MYF(0), drop->type_name(),
+ drop->name);
+ goto err;
+ }
+ }
// Leave the DROP FOREIGN KEY names in the alter_info->drop_list.
break;
}
}
+ drop_it.rewind();
}
if (!create_info->comment.str)