Using INSERT ... ON DUPLICATE KEY UPDATE on a table which has a field in a foreign key linked to a PRIMARY field, throw a foreign key error even this field is not updated.
To reproduce :
create database test;
|
use test;
|
|
CREATE TABLE `v` (
|
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
PRIMARY KEY (`id`)
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
|
INSERT v values (1);
|
|
CREATE TABLE `vp` (
|
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
`v_id` int(10) unsigned NOT NULL,
|
`p_id` int(10) unsigned NOT NULL,
|
`ppp` varchar(255) NOT NULL,
|
PRIMARY KEY (`id`),
|
UNIQUE KEY `IDX_vp_uniq` (`v_id`,`p_id`),
|
KEY `FK_vp_v` (`v_id`),
|
CONSTRAINT `FK_vp_v` FOREIGN KEY (`v_id`) REFERENCES `v` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
|
INSERT vp VALUES (12, 1, 100, 'text12');
|
|
set sql_mode='';
|
|
INSERT INTO `vp` (`id`,`ppp`) VALUES (12, 'test12-2') ON DUPLICATE KEY UPDATE `ppp` = VALUES(`ppp`);
|
Result:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`vp`, CONSTRAINT `FK_vp_v` FOREIGN KEY (`v_id`) REFERENCES `v` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
|
I can reproduce it on MariaDB 10.2.6 and 10.3.0.
There is no error on 10.1.24.
Thanks
{"report":{"fcp":1043.5,"ttfb":231.89999961853027,"pageVisibility":"visible","entityId":62000,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1f6e26e7-2a7a-4bdf-9fa2-3f13c1b6f51a","navigationType":0,"readyForUser":1130.8999996185303,"redirectCount":0,"resourceLoadedEnd":1428.3000001907349,"resourceLoadedStart":238.5999994277954,"resourceTiming":[{"duration":331.4000005722046,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":238.5999994277954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":238.5999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":570,"responseStart":0,"secureConnectionStart":0},{"duration":331.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":238.80000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":238.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":570.3000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":340,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":239.0999994277954,"connectEnd":239.0999994277954,"connectStart":239.0999994277954,"domainLookupEnd":239.0999994277954,"domainLookupStart":239.0999994277954,"fetchStart":239.0999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":239.0999994277954,"responseEnd":579.0999994277954,"responseStart":579.0999994277954,"secureConnectionStart":239.0999994277954},{"duration":460.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":239.19999980926514,"connectEnd":239.19999980926514,"connectStart":239.19999980926514,"domainLookupEnd":239.19999980926514,"domainLookupStart":239.19999980926514,"fetchStart":239.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":239.19999980926514,"responseEnd":699.8999996185303,"responseStart":699.8999996185303,"secureConnectionStart":239.19999980926514},{"duration":464.4000005722046,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":239.39999961853027,"connectEnd":239.39999961853027,"connectStart":239.39999961853027,"domainLookupEnd":239.39999961853027,"domainLookupStart":239.39999961853027,"fetchStart":239.39999961853027,"redirectEnd":0,"redirectStart":0,"requestStart":239.39999961853027,"responseEnd":703.8000001907349,"responseStart":703.8000001907349,"secureConnectionStart":239.39999961853027},{"duration":464.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":239.5999994277954,"connectEnd":239.5999994277954,"connectStart":239.5999994277954,"domainLookupEnd":239.5999994277954,"domainLookupStart":239.5999994277954,"fetchStart":239.5999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":239.5999994277954,"responseEnd":704.1999998092651,"responseStart":704.1999998092651,"secureConnectionStart":239.5999994277954},{"duration":464.79999923706055,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":239.80000019073486,"connectEnd":239.80000019073486,"connectStart":239.80000019073486,"domainLookupEnd":239.80000019073486,"domainLookupStart":239.80000019073486,"fetchStart":239.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":239.80000019073486,"responseEnd":704.5999994277954,"responseStart":704.5999994277954,"secureConnectionStart":239.80000019073486},{"duration":485,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":240,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":240,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":725,"responseStart":0,"secureConnectionStart":0},{"duration":464.9000005722046,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":240.0999994277954,"connectEnd":240.0999994277954,"connectStart":240.0999994277954,"domainLookupEnd":240.0999994277954,"domainLookupStart":240.0999994277954,"fetchStart":240.0999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":240.0999994277954,"responseEnd":705,"responseStart":705,"secureConnectionStart":240.0999994277954},{"duration":484.79999923706055,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/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":240.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":240.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":725.0999994277954,"responseStart":0,"secureConnectionStart":0},{"duration":465.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/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":240.39999961853027,"connectEnd":240.39999961853027,"connectStart":240.39999961853027,"domainLookupEnd":240.39999961853027,"domainLookupStart":240.39999961853027,"fetchStart":240.39999961853027,"redirectEnd":0,"redirectStart":0,"requestStart":240.39999961853027,"responseEnd":705.5999994277954,"responseStart":705.5999994277954,"secureConnectionStart":240.39999961853027},{"duration":628.0999994277954,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":241.5,"connectEnd":241.5,"connectStart":241.5,"domainLookupEnd":241.5,"domainLookupStart":241.5,"fetchStart":241.5,"redirectEnd":0,"redirectStart":0,"requestStart":241.5,"responseEnd":869.5999994277954,"responseStart":869.5999994277954,"secureConnectionStart":241.5},{"duration":1186.8000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":241.5,"connectEnd":241.5,"connectStart":241.5,"domainLookupEnd":241.5,"domainLookupStart":241.5,"fetchStart":241.5,"redirectEnd":0,"redirectStart":0,"requestStart":241.5,"responseEnd":1428.3000001907349,"responseStart":1428.3000001907349,"secureConnectionStart":241.5},{"duration":431.69999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":737.3000001907349,"connectEnd":737.3000001907349,"connectStart":737.3000001907349,"domainLookupEnd":737.3000001907349,"domainLookupStart":737.3000001907349,"fetchStart":737.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":737.3000001907349,"responseEnd":1169,"responseStart":1169,"secureConnectionStart":737.3000001907349},{"duration":417.29999923706055,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1037.3000001907349,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1037.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1454.5999994277954,"responseStart":0,"secureConnectionStart":0},{"duration":326.8999996185303,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":1177.5,"connectEnd":1177.5,"connectStart":1177.5,"domainLookupEnd":1177.5,"domainLookupStart":1177.5,"fetchStart":1177.5,"redirectEnd":0,"redirectStart":0,"requestStart":1177.5,"responseEnd":1504.3999996185303,"responseStart":1504.3999996185303,"secureConnectionStart":1177.5}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":31,"responseStart":232,"responseEnd":236,"domLoading":235,"domInteractive":1461,"domContentLoadedEventStart":1461,"domContentLoadedEventEnd":1509,"domComplete":2193,"loadEventStart":2193,"loadEventEnd":2193,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1433.0999994277954},{"name":"bigPipe.sidebar-id.end","time":1434},{"name":"bigPipe.activity-panel-pipe-id.start","time":1434.0999994277954},{"name":"bigPipe.activity-panel-pipe-id.end","time":1437.3999996185303},{"name":"activityTabFullyLoaded","time":1526.5}],"measures":[],"correlationId":"1ece3b0220155f","effectiveType":"4g","downlink":9.2,"rtt":0,"serverDuration":137,"dbReadsTimeInMs":14,"dbConnsTimeInMs":23,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
marko Found the offending changes on 5.7. There is actually two of them:
Author: Annamalai Gurusami <annamalai.gurusami@oracle.com> Mon Feb 24 10:30:03 2014
Committer: Annamalai Gurusami <annamalai.gurusami@oracle.com> Mon Feb 24 10:30:03 2014
Bug #17604730 ASSERTION: *CURSOR->INDEX->NAME == TEMP_INDEX_PREFIX
Problem:
When INSERT ... ON DUPLICATE UPDATE or REPLACE statements are used, then
after encountering a DB_DUPLICATE_KEY error, we continue to process all
the unique secondary indexes to place the necessary gap locks. The
problem is in the following scenario:
1. The table has one primary index, one unique secondary index and
one non-unique secondary index.
2. The INSERT ... ON DUPLICATE UPDATE ... is executed on the table.
3. Insert into the clustered index reported DB_DUPLICATE_KEY. This
error information is saved. We proceed to take gap locks in all
unique secondary indexes.
4. Insert into the unique secondary index reported DB_LOCK_WAIT.
5. Step 4 is repeated from a higher layer row_ins(). When this is
done, the earlier error information saved in step 3 is lost.
6. Next instead of taking just gap locks or skipping non-unique
secondary indexes, because of loss of information regarding the
error already saved, an actual insert is performed on the non-unique
secondary index. This triggers the assert.
Solution:
Save the error information in a non-local location so that it is not lost.
rb#4723 approved by Kevin.
Author: Marko Mäkelä <marko.makela@oracle.com> Tue Nov 27 11:12:13 2012
Committer: Marko Mäkelä <marko.makela@oracle.com> Tue Nov 27 11:12:13 2012
Bug#15920445 INNODB REPORTS ER_DUP_KEY BEFORE CREATE UNIQUE INDEX COMPLETED
There is a phase during online secondary index creation where the index has
been internally completed inside InnoDB, but does not 'officially' exist yet.
We used to report ER_DUP_KEY in these situations, like this:
ERROR 23000: Can't write; duplicate key in table 't1'
What we should do is to let the 'offending' operation complete, but report an
error to the
ALTER TABLE t1 ADD UNIQUE KEY (c2):
ERROR HY000: Index c2 is corrupted
(This misleading error message should be fixed separately:
Bug#15920713 CREATE UNIQUE INDEX REPORTS ER_INDEX_CORRUPT INSTEAD OF DUPLICATE)
row_ins_sec_index_entry_low(): flag the index corrupted instead of
reporting a duplicate, in case the index has not been published yet.
rb:1614 approved by Jimmy Yang
Problem is that we found the duplicate key error on primary key but then also we found DB_NO_REFERENCED_ROW as search for v_id value 0 fails from index IDX_vp_uniq. This leads wrong error code returned from row_ins().