I have a really weird problem with MySQL where ON UPDATE CASCADE is behaving like ON UPDATE RESTRICT.
I have two MySQL servers, an old Windows Server 2012 and a new Ubuntu 16.04 LTS.
On both servers, I have three tables -- item, invoice and invoice_box
CREATE TABLE `item` (
`id` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`idInvoice` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`boxNumber` tinyint(4) NULL DEFAULT NULL,
CONSTRAINT `fk_gauge_item_invoice` FOREIGN KEY (`idInvoice`) REFERENCES `invoice` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk_gauge_item_invoice_box` FOREIGN KEY (`idInvoice`, `boxNumber`) REFERENCES `invoice_box` (`idInvoice`, `boxNumber`) ON DELETE SET NULL ON UPDATE CASCADE
)
CREATE TABLE `invoice_box` (
`idInvoice` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`boxNumber` tinyint(4) NOT NULL,
CONSTRAINT `fk_gauge_box_invoice` FOREIGN KEY (`idInvoice`) REFERENCES `invoice` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
CREATE TABLE `invoice` (
`id` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
)
On my old Windows server, if I update the invoice id, the update cascades to the item and invoice_box table successfully.
On the Ubuntu server, it works only if
item
table, then it cascades to invoice_box
table correctly. invoice_box
table, then it cascades to item
table correctly. If there are entries in both item
and invoice_box
table, then it fails with the following message.
1452 - Cannot add or update a child row; a foreign key constraint fails (product.item CONSTRAINT fk_item_invoice FOREIGN KEY (idInvoice) REFERENCES invoice(id) ON DELETE SET NULL ON UPDATE CASCADE)
I don't understand why this won't work exactly the same on both servers. I did a dump from the Windows server to migrate onto the Ubuntu server, if that makes any difference.
Code works as coded with the removal of fk_gauge_item_invoice
drop table if exists i;
drop table if exists ib;
drop table if exists inv;
CREATE TABLE `inv` (
`id` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
primary key (id)
);
CREATE TABLE `ib` (
`idInvoice` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`boxNumber` tinyint(4) NOT NULL,
key ib1(idinvoice,boxnumber),
CONSTRAINT `fk_gauge_box_invoice` FOREIGN KEY (`idInvoice`) REFERENCES `inv` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE `i` (
`id` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`idInvoice` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`boxNumber` tinyint(4) NULL DEFAULT NULL,
#CONSTRAINT `fk_gauge_item_invoice` FOREIGN KEY (`idInvoice`) REFERENCES `inv` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk_gauge_item_invoice_box` FOREIGN KEY (`idInvoice`, `boxNumber`) REFERENCES `ib` (`idInvoice`, `boxNumber`) ON DELETE SET NULL ON UPDATE CASCADE
);
insert into inv values (1),(2),(3);
insert into ib values (1,1),(1,2),(2,1),(2,2),(2,3);
insert into i values (1,1,1),(1,1,2),(2,1,1),(2,2,2),(2,2,3);
MariaDB [sandbox]> update inv set id = 10 where id = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from inv;
+----+
| id |
+----+
| 10 |
| 2 |
| 3 |
+----+
3 rows in set (0.00 sec)
MariaDB [sandbox]> select * from ib;
+-----------+-----------+
| idInvoice | boxNumber |
+-----------+-----------+
| 10 | 1 |
| 10 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
+-----------+-----------+
5 rows in set (0.00 sec)
MariaDB [sandbox]> select * from i;
+----+-----------+-----------+
| id | idInvoice | boxNumber |
+----+-----------+-----------+
| 1 | 10 | 1 |
| 1 | 10 | 2 |
| 2 | 10 | 1 |
| 2 | 2 | 2 |
| 2 | 2 | 3 |
+----+-----------+-----------+
5 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> update ib set idinvoice = 20 where idinvoice = 10;
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`sandbox`.`ib`, CONSTRAINT `fk_gauge_box_invoice` FOREIGN KEY (`idInvoice`) REFERENCES `inv` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
So I'm not clear what your problem is - if you can set up a sql fiddle to demonstrate your issue then that would help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.