[MDEV-23822] Deleting unique constraint with column in fk breaks inserts Created: 2020-09-25  Updated: 2020-10-06  Resolved: 2020-09-28

Status: Closed
Project: MariaDB Server
Component/s: Storage Engine - InnoDB
Affects Version/s: 10.5.5
Fix Version/s: 10.2.35, 10.3.26, 10.4.16, 10.5.7

Type: Bug Priority: Major
Reporter: Ilya Sheershoff Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Environment:

docker image:
mariadb:10.5.5 (10.5.5-MariaDB-1:10.5.5+maria~focal)
Debian GNU/Linux 9 Linux vm-5ce94a45 4.17.0-0.bpo.3-amd64 #1 SMP Debian 4.17.17-1~bpo9+1 (2018-08-27) x86_64 GNU/Linux
10.5.5-MariaDB-1:10.5.5+maria~stretch


Issue Links:
Duplicate
duplicates MDEV-23470 InnoDB: Failing assertion: cmp < 0 in... Closed

 Description   

Upgraded to 10.5.5 from 10.2.2 because of MDEV-20723 . Still having an issue, though, with the same tables.

Haven't checked this on MySQL. Checked just the 10.5.5 version in the docker image and debian system.

Kinda minimal code to reproduce:

DROP DATABASE testmaria;
CREATE DATABASE IF NOT EXISTS testmaria;
SELECT VERSION();
USE testmaria;
create table organization
(
    id          int auto_increment comment '#|hidden'
        primary key
)
    comment 'Organizations' charset = utf8;
create table menu
(
    id               int auto_increment comment '#|hidden'
        primary key,
    organization_id  int  null comment 'Organization',
    date_realization date null comment 'Date realization',
    constraint `add-menu`
        unique (organization_id, date_realization),
    constraint `menu-fk`
        foreign key (organization_id) references organization (id)
            on update cascade on delete cascade
)
    comment 'Menu' charset = utf8;
INSERT INTO organization (id)
    VALUES (5);
INSERT INTO `menu` (`organization_id`, `date_realization`)
    VALUES (5, '2020-09-27');
SET foreign_key_checks = 0;
ALTER TABLE menu
    DROP INDEX `add-menu`;
SET foreign_key_checks = 1;
-- Neither this works
INSERT INTO `menu` (`organization_id`, `date_realization`)
    VALUES (5, '2020-09-27');
-- Nor this
INSERT INTO `menu` (`organization_id`, `date_realization`)
    VALUES (5, '2020-09-28');

Version output and errors, logs for successful statements skipped:

10.5.5-MariaDB-1:10.5.5+maria~focal
testmaria> INSERT INTO `menu` (`organization_id`, `date_realization`)
               VALUES (5, '2020-09-27')
[2020-09-25 20:07:04] [23000][1452] (conn=21) Cannot add or update a child row: a foreign key constraint fails (`testmaria`.`menu`, CONSTRAINT `menu-fk` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
testmaria> INSERT INTO `menu` (`organization_id`, `date_realization`)
               VALUES (5, '2020-09-28')
[2020-09-25 20:07:11] [23000][1452] (conn=21) Cannot add or update a child row: a foreign key constraint fails (`testmaria`.`menu`, CONSTRAINT `menu-fk` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

What i'd like to see:

last two queries successfully insert data

What helps:

Recreating the table with new structure and the data. After executing the statements from above, doing this fixes the inserts:

SET foreign_key_checks = 0;
DROP TABLE menu;
create table menu
(
    id               int auto_increment comment '#|hidden'
        primary key,
    organization_id  int  null comment 'Organization',
    date_realization date null comment 'Date realization',
    constraint `menu-fk`
        foreign key (organization_id) references organization (id)
            on update cascade on delete cascade
)
    comment 'Menu' charset = utf8;
INSERT INTO `menu` (`organization_id`, `date_realization`)
VALUES (5, '2020-09-27');
SET foreign_key_checks = 1;
-- Now this works
INSERT INTO `menu` (`organization_id`, `date_realization`)
VALUES (5, '2020-09-27');
-- And this
INSERT INTO `menu` (`organization_id`, `date_realization`)
VALUES (5, '2020-09-28');

How I fix this in production:

For those who stumbles upon and doesn't know how to fix.
NB! I have a pretty small table with the problem - just several Kbs, if you have big problematic table - consider checking that you have enough space.

1. check that you have the corresponding CREATE TABLE statement for the current database state for the problematic table.
1. disconnect apps, make backups
1. set foreign_key_checks = 0; select * from [TABLE] into outfile '/tmp/DB_TABLE.csv'; drop table [TABLE]; create table [TABLE] [here goes the DDL you should have from previous lines]; load data infile '/tmp/DB_TABLE.csv' into table TABLE;



 Comments   
Comment by Alice Sherepa [ 2020-09-28 ]

Thanks a lot! I reproduced the bug on 10.2.33, 10.5.5
On the current versions 10.2-10.5 works as expected.
It has been fixed by 837bbbafc535d14ea1 commit (fixing MDEV-23470)

commit 837bbbafc535d14ea102ced983349fd29b84f782 (HEAD)
Author: Thirunarayanan Balathandayuthapani <thiru@mariadb.com>
Date:   Wed Sep 2 16:15:02 2020 +0530
 
    MDEV-23470 InnoDB: Failing assertion: cmp < 0 in row_ins_check_foreign_constraint
    
    During insertion of clustered index, InnoDB does the check for foreign key
    constraints. Problem is that it uses the clustered index entry to search
    indexes of referenced tables and it could lead to unexpected result
    when there is no foreign index.
    
    Solution:
    ========
      Rebuild the tuple based on foreign column names before searching it
    on reference index when there is no foreign index.

Generated at Thu Feb 08 09:25:18 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.