MDEV-33927 Wrong error message while altering the table with foreign key relationship

- Alter statement using copy algorithm throws wrong message
"Cannot delete rows from table which is parent in a foreign
key constraint" even though we expect duplicate key for the index.

copy_data_between_tables(): Remove the error condition to
throw ER_FK_CANNOT_DELETE_PARENT and make sure that ALTER IGNORE
shouldn't allow to break unique of foreign key constraints.
parent 662bb507
......@@ -132,3 +132,20 @@ ADD CONSTRAINT t2_fk FOREIGN KEY(f3) REFERENCES t1(f1);
DROP TABLE t2, t1;
SET SESSION FOREIGN_KEY_CHECKS = ON;
# End of 10.4 tests
#
# MDEV-33927 Wrong error message while altering the table with
# foreign key relationship
#
CREATE TABLE t1(f1 INT NOT NULL, f2 INT NOT NULL,
f3 INT NOT NULL, PRIMARY KEY(f1),
INDEX(f2))ENGINE=InnoDB;
INSERT INTO t1 VALUES(1, 1, 2), (2, 2, 2);
CREATE TABLE t2 (f1 INT NOT NULL, f2 INT NOT NULL,
PRIMARY KEY(f1), INDEX(f2)) ENGINE=INNODB;
INSERT INTO t2 VALUES(1, 1), (2, 2);
ALTER TABLE t1 ADD FOREIGN KEY(f1) REFERENCES t1(f1);
ALTER TABLE t1 ADD FOREIGN KEY (f2) REFERENCES t2 (f2),
ADD UNIQUE INDEX(f3), ALGORITHM=COPY;
ERROR 23000: Duplicate entry '2' for key 'f3'
DROP TABLE t1, t2;
# End of 10.5 tests
......@@ -168,3 +168,21 @@ ALTER TABLE t2 CHANGE COLUMN f2 f3 VARCHAR(20) NOT NULL,
DROP TABLE t2, t1;
SET SESSION FOREIGN_KEY_CHECKS = ON;
--echo # End of 10.4 tests
--echo #
--echo # MDEV-33927 Wrong error message while altering the table with
--echo # foreign key relationship
--echo #
CREATE TABLE t1(f1 INT NOT NULL, f2 INT NOT NULL,
f3 INT NOT NULL, PRIMARY KEY(f1),
INDEX(f2))ENGINE=InnoDB;
INSERT INTO t1 VALUES(1, 1, 2), (2, 2, 2);
CREATE TABLE t2 (f1 INT NOT NULL, f2 INT NOT NULL,
PRIMARY KEY(f1), INDEX(f2)) ENGINE=INNODB;
INSERT INTO t2 VALUES(1, 1), (2, 2);
ALTER TABLE t1 ADD FOREIGN KEY(f1) REFERENCES t1(f1);
--error ER_DUP_ENTRY
ALTER TABLE t1 ADD FOREIGN KEY (f2) REFERENCES t2 (f2),
ADD UNIQUE INDEX(f3), ALGORITHM=COPY;
DROP TABLE t1, t2;
--echo # End of 10.5 tests
......@@ -11857,23 +11857,7 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to, bool ignore,
}
else
{
/* Duplicate key error. */
if (unlikely(alter_ctx->fk_error_if_delete_row))
{
/*
We are trying to omit a row from the table which serves as parent
in a foreign key. This might have broken referential integrity so
emit an error. Note that we can't ignore this error even if we are
executing ALTER IGNORE TABLE. IGNORE allows to skip rows, but
doesn't allow to break unique or foreign key constraints,
*/
my_error(ER_FK_CANNOT_DELETE_PARENT, MYF(0),
alter_ctx->fk_error_id,
alter_ctx->fk_error_table);
break;
}
if (ignore)
if (ignore && !alter_ctx->fk_error_if_delete_row)
{
/* This ALTER IGNORE TABLE. Simply skip row and continue. */
to->file->restore_auto_increment(prev_insert_id);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment