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

Foreign key cannot be used in the check constraint

    XMLWordPrintable

Details

    Description

      Hi

      I have following code snippet, where I use a foreign key in a check constraint. This code was working with version 10.10.2, but does not anymore with 10.10.3. I checked and it still works with the latest 10.3.38.

      DROP TABLE IF EXISTS `feedback`;
       
      CREATE TABLE IF NOT EXISTS `feedback` (
          `id` INT NOT NULL AUTO_INCREMENT,
          PRIMARY KEY (`id`));
         
      DROP TABLE IF EXISTS `message`;
       
      CREATE TABLE IF NOT EXISTS `message` (
      	`id` INT NOT NULL AUTO_INCREMENT,
      	`feedback_id` INT,
      	`value` INT,
      	PRIMARY KEY (`id`),
      	CONSTRAINT `fk_message_feedback`
      		FOREIGN KEY (`feedback_id`)
      			REFERENCES `feedback` (`id`)
      			ON UPDATE CASCADE,
      	CONSTRAINT `failing_constraint`
      		CHECK ((`value` >= 10 AND `feedback_id` IS NOT NULL)
      		OR (`value` <= 10 AND `feedback_id` IS NULL)));
      

      I get the error code 1901:

      Error Code: 1901. Function or expression 'feedback_id' cannot be used in the CHECK clause of `failing_constraint`
      

      Now my question is, if you have introduced a bug in your latest release or if that code snippet is something that should not be working, but was working so far unintentionally. I did not find an exact documentation if a foreign key should not be used in a check constraint.

      Attachments

        Issue Links

          Activity

            People

              serg Sergei Golubchik
              Vulin Nikola
              Votes:
              2 Vote for this issue
              Watchers:
              8 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.