Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-11277

REPLACE query leads to 'duplicate error for key PRIMARY' mesage, but must be 'foreign key constraint fail'.

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Trivial
    • Resolution: Cannot Reproduce
    • None
    • N/A
    • OTHER
    • None

    Description

      Consider the following example. We just create two tables, second with the foreign key, and our foreign key is also a part of compound primary key of the second table:

      CREATE TABLE t
          (
            `id` int,
            `productName` varchar(70),
            primary key (id)
          );
       
      CREATE TABLE t2
          (`id` int, `title` varchar(70), `fk_t` int,
           primary key (id, fk_t),
           constraint `t2_to_t_fkz` foreign key (`fk_t`) references `t`(`id`));
         
      insert into t values(1, 'product 1');
      replace into t2(title, fk_t) values('incorrect fk, replace syntax', 9999);
      
      

      Last replace is an error, because the foreign key called fk_t leads to nowhere and thats why new row can't be inserted. But the server behavior varies on MySQL and MariaDB.

      MySQL says:

      Cannot add or update a child row: a foreign key constraint fails (`db_9_63d471`.`t2`, CONSTRAINT `t2_to_t_fkz` FOREIGN KEY (`fk_t`) REFERENCES `t` (`id`))
      

      and that is correct error message.

      But MariaDB says:

      Error Code: 1062. Duplicate entry '0-9999' for key 'PRIMARY'
      

      What? Duplicate entry? But i am using REPLACE query, you should just delete a row and insert again in such a case. The error messsage is wrong and confusing.

      Attachments

        Activity

          People

            Unassigned Unassigned
            vpyatykh Vasiliy Pyatykh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.