Type:
Bug
Priority:
Minor
Resolution:
Fixed
Affects Version/s:
10.3(EOL) , 10.4(EOL) , 10.5 , 10.6 , 10.7(EOL) , 10.8(EOL) , 10.9(EOL) , 10.10(EOL) , 10.11 , 11.0(EOL)
Setting to minor because a foreign key on a column referencing itself doesn't make much sense. But possibly a more meaningful use case can be invented based on this.
For a foreign key to self-reference a column, the table should have two keys on the column, otherwise FK creation fails:
10.3 96be3fe8
MariaDB [test]> create table t (a int , key (a), foreign key (a) references t(a)) engine=InnoDB;
ERROR 1005 (HY000): Can 't create table `test`.`t` (errno: 150 "Foreign key constraint is incorrectly formed")
MariaDB [test]> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 150 | Create table `test`.`t` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' foreign key (a) references t(a)) engine=InnoDB '. |
| Error | 1005 | Can' t create table `test`.`t` (errno: 150 "Foreign key constraint is incorrectly formed" ) |
| Warning | 1215 | Cannot add foreign key constraint for `t` |
+ ---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.000 sec)
Instead it has to be done like this:
MariaDB [test]> create table t (a int , key (a), key (a), foreign key (a) references t(a)) engine=InnoDB;
Query OK, 0 rows affected, 1 warning (0.068 sec)
(the warning is about duplicate keys being deprecated, but it's not important in the context of this issue).
So, we get this structure:
MariaDB [test]> show create table t \G
*************************** 1. row ***************************
Table : t
Create Table : CREATE TABLE `t` (
`a` int (11) DEFAULT NULL ,
KEY `a` (`a`),
KEY `a_2` (`a`),
CONSTRAINT `t_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.001 sec)
Now we can drop the second key, as long as the algorithm is anything but COPY, the FOREIGN_KEY_CHECKS doesn't prevent it:
MariaDB [test]> select @@foreign_key_checks;
+ ----------------------+
| @@foreign_key_checks |
+ ----------------------+
| 1 |
+ ----------------------+
1 row in set (0.000 sec)
MariaDB [test]> alter table t drop key a_2;
Query OK, 0 rows affected (0.029 sec)
Records: 0 Duplicates: 0 Warnings: 0
And we get this:
MariaDB [test]> show create table t \G
*************************** 1. row ***************************
Table : t
Create Table : CREATE TABLE `t` (
`a` int (11) DEFAULT NULL ,
KEY `a` (`a`),
CONSTRAINT `t_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.000 sec)
As we already know, it is an invalid structure, it can't be used to re-create the table (not even with FOREIGN_KEY_CHECKS=0, so if it happens to be in a dump, it will be broken):
MariaDB [test]> drop table if exists t;
Query OK, 0 rows affected (0.033 sec)
MariaDB [test]> set foreign_key_checks=0;
Query OK, 0 rows affected (0.000 sec)
MariaDB [test]> CREATE TABLE `t` (
-> `a` int (11) DEFAULT NULL ,
-> KEY `a` (`a`),
-> CONSTRAINT `t_ibfk_1` FOREIGN KEY (`a`) REFERENCES `t` (`a`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ERROR 1005 (HY000): Can 't create table `test`.`t` (errno: 150 "Foreign key constraint is incorrectly formed")
MariaDB [test]> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 150 | Create table `test`.`t` with foreign key constraint failed. There is no index in the referenced table where the referenced columns appear as the first columns near ' FOREIGN KEY (`a`) REFERENCES `t` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 '. |
| Error | 1005 | Can' t create table `test`.`t` (errno: 150 "Foreign key constraint is incorrectly formed" ) |
| Warning | 1215 | Cannot add foreign key constraint for `t` |
+ ---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.000 sec)
{"report":{"fcp":1010.6000003814697,"ttfb":264.6000003814697,"pageVisibility":"visible","entityId":112724,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"039b40c7-8480-4879-9d03-4152e0940aa0","navigationType":0,"readyForUser":1112.6999998092651,"redirectCount":0,"resourceLoadedEnd":1231.8000001907349,"resourceLoadedStart":270.8999996185303,"resourceTiming":[{"duration":249.60000038146973,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":270.8999996185303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":270.8999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":520.5,"responseStart":0,"secureConnectionStart":0},{"duration":249.69999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":271.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":271.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":520.8000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":258.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":271.30000019073486,"connectEnd":271.30000019073486,"connectStart":271.30000019073486,"domainLookupEnd":271.30000019073486,"domainLookupStart":271.30000019073486,"fetchStart":271.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":271.30000019073486,"responseEnd":530,"responseStart":530,"secureConnectionStart":271.30000019073486},{"duration":314.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":271.5,"connectEnd":271.5,"connectStart":271.5,"domainLookupEnd":271.5,"domainLookupStart":271.5,"fetchStart":271.5,"redirectEnd":0,"redirectStart":0,"requestStart":271.5,"responseEnd":586.1999998092651,"responseStart":586.1999998092651,"secureConnectionStart":271.5},{"duration":318.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":271.80000019073486,"connectEnd":271.80000019073486,"connectStart":271.80000019073486,"domainLookupEnd":271.80000019073486,"domainLookupStart":271.80000019073486,"fetchStart":271.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":271.80000019073486,"responseEnd":590,"responseStart":590,"secureConnectionStart":271.80000019073486},{"duration":318.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":272,"connectEnd":272,"connectStart":272,"domainLookupEnd":272,"domainLookupStart":272,"fetchStart":272,"redirectEnd":0,"redirectStart":0,"requestStart":272,"responseEnd":590.5,"responseStart":590.5,"secureConnectionStart":272},{"duration":318.79999923706055,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":272.1000003814697,"connectEnd":272.1000003814697,"connectStart":272.1000003814697,"domainLookupEnd":272.1000003814697,"domainLookupStart":272.1000003814697,"fetchStart":272.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":272.1000003814697,"responseEnd":590.8999996185303,"responseStart":590.8999996185303,"secureConnectionStart":272.1000003814697},{"duration":401.0999994277954,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":272.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":272.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":673.3999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":319.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":272.5,"connectEnd":272.5,"connectStart":272.5,"domainLookupEnd":272.5,"domainLookupStart":272.5,"fetchStart":272.5,"redirectEnd":0,"redirectStart":0,"requestStart":272.5,"responseEnd":591.6000003814697,"responseStart":591.6000003814697,"secureConnectionStart":272.5},{"duration":400.8999996185303,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":272.6000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":272.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":673.5,"responseStart":0,"secureConnectionStart":0},{"duration":319.3999996185303,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":272.80000019073486,"connectEnd":272.80000019073486,"connectStart":272.80000019073486,"domainLookupEnd":272.80000019073486,"domainLookupStart":272.80000019073486,"fetchStart":272.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":272.80000019073486,"responseEnd":592.1999998092651,"responseStart":592.1999998092651,"secureConnectionStart":272.80000019073486},{"duration":917,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":279.1000003814697,"connectEnd":279.1000003814697,"connectStart":279.1000003814697,"domainLookupEnd":279.1000003814697,"domainLookupStart":279.1000003814697,"fetchStart":279.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":279.1000003814697,"responseEnd":1196.1000003814697,"responseStart":1196.1000003814697,"secureConnectionStart":279.1000003814697},{"duration":914.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":282.3999996185303,"connectEnd":282.3999996185303,"connectStart":282.3999996185303,"domainLookupEnd":282.3999996185303,"domainLookupStart":282.3999996185303,"fetchStart":282.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":282.3999996185303,"responseEnd":1196.5,"responseStart":1196.5,"secureConnectionStart":282.3999996185303},{"duration":216.89999961853027,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":685,"connectEnd":685,"connectStart":685,"domainLookupEnd":685,"domainLookupStart":685,"fetchStart":685,"redirectEnd":0,"redirectStart":0,"requestStart":685,"responseEnd":901.8999996185303,"responseStart":901.8999996185303,"secureConnectionStart":685},{"duration":286.9000005722046,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":944.8999996185303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":944.8999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1231.8000001907349,"responseStart":0,"secureConnectionStart":0}],"fetchStart":1,"domainLookupStart":1,"domainLookupEnd":1,"connectStart":1,"connectEnd":1,"requestStart":92,"responseStart":265,"responseEnd":283,"domLoading":269,"domInteractive":1255,"domContentLoadedEventStart":1255,"domContentLoadedEventEnd":1299,"domComplete":2867,"loadEventStart":2867,"loadEventEnd":2869,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1237.6000003814697},{"name":"bigPipe.sidebar-id.end","time":1238.3999996185303},{"name":"bigPipe.activity-panel-pipe-id.start","time":1238.5},{"name":"bigPipe.activity-panel-pipe-id.end","time":1240.8999996185303},{"name":"activityTabFullyLoaded","time":1306.5}],"measures":[],"correlationId":"4a7c1196df32cd","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":102,"dbReadsTimeInMs":10,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}