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

FKs with 'ON UPDATE CASCADE' can be set in some contexts but not others

    XMLWordPrintable

Details

    Description

      First of all - sorry if this should be a comment on the related ticket as opposed to its own ticket; however my report goes further (/has a wider issue than the original report / causes problems with backups); so I believed best to raise as its own ticket.

      As per MDEV-30606, trying to create a table with a FK that has ON UPDATE CASCADE and a CHECK CONSTRAINT returns the (slightly unhelpful) error message: Function or expression %s cannot be used in the CHECK clause of %s. (It would be useful if this error was less generic for the use case, as it is known that the referred column is not a function or expression - but the error makes it seem like you have used the wrong column name).

      However running `ALTER TABLE` lets you add the constraint without issue (and then the `CREATE TABLE` statement will fail, breaking restoring backups or copying the table/etc).

      Test Script:

      # Setup
      CREATE OR REPLACE TABLE `x1` (
      	`a` INT NOT NULL, 
      	PRIMARY KEY(`a`)
      );
      # Fails with 'Function or expression 'b' cannot be used in the CHECK clause of `b-2`'
      CREATE OR REPLACE TABLE `x2` (
      	`b` INT NULL,  
      	CONSTRAINT FOREIGN KEY(`b`) REFERENCES `x1`(`a`) ON UPDATE CASCADE, 
      	CONSTRAINT CHECK (`b` > 1)
      );
      # Succeeds
      CREATE OR REPLACE TABLE `x2` (
      	`b` INT NULL,  
      	CONSTRAINT FOREIGN KEY(`b`) REFERENCES `x1`(`a`) ON UPDATE CASCADE
      );
      ALTER TABLE `x2` ADD CONSTRAINT CHECK (`b` > 1);
      # Then:
      SHOW CREATE TABLE `x2`;
      /* Outputs, but running this will error as per the initial create command
      CREATE TABLE `x2` (
        `b` int(11) DEFAULT NULL,
        KEY `b` (`b`),
        CONSTRAINT `x2_ibfk_1` FOREIGN KEY (`b`) REFERENCES `x1` (`a`) ON UPDATE CASCADE,
        CONSTRAINT `CONSTRAINT_1` CHECK (`b` > 1)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci
      */
      

      Attachments

        Issue Links

          Activity

            People

              serg Sergei Golubchik
              rmhumphries Robert Humphries
              Votes:
              0 Vote for this issue
              Watchers:
              3 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.