Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The behavior of TiDB foreign keys is inconsistent with MySQL #44848

Closed
yao-2110 opened this issue Jun 20, 2023 · 3 comments · Fixed by #44907
Closed

The behavior of TiDB foreign keys is inconsistent with MySQL #44848

yao-2110 opened this issue Jun 20, 2023 · 3 comments · Fixed by #44907
Assignees
Labels
affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.2 compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) severity/moderate type/bug The issue is confirmed as a bug. type/compatibility

Comments

@yao-2110
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table b (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `f` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

create table a (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `b_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_b_id` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

insert b(id,f) values(1,1);
insert a(id,b_id) values(1,1);
update b set id=1,f=2 where id=1;

2. What did you expect to see? (Required)

mysql> use test;
Database changed
mysql> create table b (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `f` int(11) NOT NULL,
    -> PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.04 sec)

mysql> create table a (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `b_id` int(11) NOT NULL,
    ->  PRIMARY KEY (`id`),
    ->   CONSTRAINT `fk_b_id` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.07 sec)

mysql> insert b(id,f) values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> insert a(id,b_id) values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> update b set id=1,f=2 where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.36-log |
+------------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> use test;
Database changed
mysql> create table b (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `f` int(11) NOT NULL,
    -> PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.52 sec)

mysql> create table a (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    -> `b_id` int(11) NOT NULL,
    ->  PRIMARY KEY (`id`),
    ->  CONSTRAINT `fk_b_id` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.52 sec)

mysql> insert b(id,f) values(1,1);
Query OK, 1 row affected (0.01 sec)

mysql> insert a(id,b_id) values(1,1);
Query OK, 1 row affected (0.02 sec)

mysql> update b set id=1,f=2 where id=1;
**ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`a`, CONSTRAINT `fk_b_id` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE)**

mysql> select version();
+--------------------+
| version()          |
+--------------------+
| 5.7.25-TiDB-v7.1.0 |
+--------------------+
1 row in set (0.00 sec)

4. What is your TiDB version? (Required)

mysql> select tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                                               |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v7.1.0
Edition: Community
Git Commit Hash: 635a4362235e8a3c0043542e629532e3c7bb2756
Git Branch: heads/refs/tags/v7.1.0
UTC Build Time: 2023-05-30 10:58:57
GoVersion: go1.20.3
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

@yao-2110 yao-2110 added the type/bug The issue is confirmed as a bug. label Jun 20, 2023
@dveeden dveeden changed the title The behavior of Tidb foreign keys is inconsistent with MySQL The behavior of TiDB foreign keys is inconsistent with MySQL Jun 22, 2023
@dveeden dveeden added severity/moderate affects-7.1 This bug affects the 7.1.x(LTS) versions. type/compatibility compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) labels Jun 22, 2023
@crazycs520 crazycs520 self-assigned this Jun 25, 2023
@borissavelev
Copy link

Please include this in 7.1.1 🙏

@crazycs520
Copy link
Contributor

Please include this in 7.1.1 🙏

#44928 is merged, v7.1.1 will contain this fix.

@dveeden
Copy link
Contributor

dveeden commented Jul 24, 2023

Confirming this is fixed:

sql> create table b (
   ->   `id` int(11) NOT NULL AUTO_INCREMENT,
   ->   `f` int(11) NOT NULL,
   ->   PRIMARY KEY (`id`)
   -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.1686 sec)

sql> 

sql> create table a (
   ->   `id` int(11) NOT NULL AUTO_INCREMENT,
   ->   `b_id` int(11) NOT NULL,
   ->   PRIMARY KEY (`id`),
   ->   CONSTRAINT `fk_b_id` FOREIGN KEY (`b_id`) REFERENCES `b` (`id`) ON DELETE CASCADE
   -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.2429 sec)

sql> 

sql> insert b(id,f) values(1,1);
Query OK, 1 row affected (0.0311 sec)

sql> insert a(id,b_id) values(1,1);
Query OK, 1 row affected (0.0151 sec)

sql> update b set id=1,f=2 where id=1;
Query OK, 1 row affected (0.0185 sec)

Rows matched: 1  Changed: 1  Warnings: 0

sql> SELECT TIDB_VERSION()\G
*************************** 1. row ***************************
TIDB_VERSION(): Release Version: v7.1.1
Edition: Community
Git Commit Hash: cf441574864be63938524e7dfcf7cc659edc3dd8
Git Branch: heads/refs/tags/v7.1.1
UTC Build Time: 2023-07-19 10:20:53
GoVersion: go1.20.6
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv
1 row in set (0.0010 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.2 compatibility-mysql8 This is a compatibility issue with MySQL 8.0(but NOT 5.7) severity/moderate type/bug The issue is confirmed as a bug. type/compatibility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants