Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
10.5, 10.6, 10.11, 10.6.12, 10.4(EOL), 10.9(EOL), 10.10(EOL)
-
None
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
- is duplicated by
-
MDEV-31769 Adding a check constraint in "create table" doesn't work if it uses a foreign key with "update cascade"
- Closed
- is part of
-
MDEV-18114 Foreign Key Constraint actions don't affect Virtual Column
- Closed
- relates to
-
MDEV-30606 Foreign key cannot be used in the check constraint
- Closed