[MDEV-31322] FKs with 'ON UPDATE CASCADE' can be set in some contexts but not others Created: 2023-05-21  Updated: 2023-08-19  Resolved: 2023-08-19

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Alter Table, Data Definition - Create Table
Affects Version/s: 10.4, 10.6.12, 10.5, 10.6, 10.9, 10.10, 10.11
Fix Version/s: 10.4.31, 10.5.22, 10.6.15, 10.9.8, 10.10.6, 10.11.5, 11.0.3

Type: Bug Priority: Major
Reporter: Robert Humphries Assignee: Sergei Golubchik
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Duplicate
is duplicated by MDEV-31769 Adding a check constraint in "create ... Closed
PartOf
is part of MDEV-18114 Foreign Key Constraint actions don't ... Closed
Relates
relates to MDEV-30606 Foreign key cannot be used in the che... Closed

 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
*/



 Comments   
Comment by Sergei Golubchik [ 2023-08-19 ]

fixed in MDEV-18114

Generated at Thu Feb 08 10:22:58 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.