MariaDB "ON UPDATE CASCADE" 约束没有按预期工作?
MariaDB "ON UPDATE CASCADE" CONSTRAINT not working as expected?
我原以为 ON UPDATE CASCADE
约束会让我更新引用的外键的值并将其级联到它的裁判(就像在 PostgreSQL 中那样),但这似乎不是案例.
我是不是漏掉了什么?
这里使用MariaDB 10.3.29-MariaDB-0ubuntu0.20.04.1
CREATE TABLE category (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
parent_id int(11) UNSIGNED DEFAULT NULL,
slug varchar(255) NOT NULL,
title varchar(255) NOT NULL,
content longtext NOT NULL,
created_at datetime NOT NULL DEFAULT current_timestamp(),
updated_at datetime DEFAULT NULL,
CONSTRAINT fk_category_parent_id FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `category` (`id`, `parent_id`, `slug`, `title`, `content`, `created_at`, `updated_at`) VALUES
(1, NULL, 'non-classe', 'Non Classé', '', '2021-05-30 16:46:52', NULL),
(2, NULL, 'cours', 'Cours', '', '2021-05-30 16:47:38', NULL),
(3, 2, 'mysql', 'MySQL', '', '2021-05-30 16:48:18', NULL),
(4, 3, 'initiation-mysql', 'Cours initiation MySQL', '', '2021-05-30 16:49:09', NULL);
UPDATE category SET id = 12 WHERE id = 2;
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`cours_cms`.`category`, CONSTRAINT `fk_category_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`) ON UPDATE CASCADE)
查看本页评论:
https://mariadb.com/kb/en/foreign-keys/
If ON UPDATE CASCADE recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE operations. This is to prevent infinite loops resulting from cascaded updates.
换句话说,如果它是 table 的分层数据类型,则 ON UPDATE CASCADE 不起作用。
只是确认这在没有 'infinite loops'
的 PostgreSQL 中有效
CREATE TABLE category (
id SERIAL PRIMARY KEY,
parent_id int DEFAULT NULL,
slug varchar(255) NOT NULL,
title varchar(255) NOT NULL,
content text NOT NULL,
CONSTRAINT fk_category_parent_id FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO category (id, parent_id, slug, title, content) VALUES
(1, NULL, 'non-classe', 'Non Classé', ''),
(2, NULL, 'cours', 'Cours', ''),
(3, 2, 'mysql', 'MySQL', ''),
(4, 3, 'initiation-mysql', 'Cours initiation MySQL', '');
UPDATE category SET id=12 WHERE id=2;
SELECT * FROM category;
id | parent_id | slug | title | content
----+-----------+------------------+------------------------+---------
1 | | non-classe | Non Classé |
4 | 3 | initiation-mysql | Cours initiation MySQL |
12 | | cours | Cours |
3 | 12 | mysql | MySQL |
我原以为 ON UPDATE CASCADE
约束会让我更新引用的外键的值并将其级联到它的裁判(就像在 PostgreSQL 中那样),但这似乎不是案例.
我是不是漏掉了什么?
这里使用MariaDB 10.3.29-MariaDB-0ubuntu0.20.04.1
CREATE TABLE category (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
parent_id int(11) UNSIGNED DEFAULT NULL,
slug varchar(255) NOT NULL,
title varchar(255) NOT NULL,
content longtext NOT NULL,
created_at datetime NOT NULL DEFAULT current_timestamp(),
updated_at datetime DEFAULT NULL,
CONSTRAINT fk_category_parent_id FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `category` (`id`, `parent_id`, `slug`, `title`, `content`, `created_at`, `updated_at`) VALUES
(1, NULL, 'non-classe', 'Non Classé', '', '2021-05-30 16:46:52', NULL),
(2, NULL, 'cours', 'Cours', '', '2021-05-30 16:47:38', NULL),
(3, 2, 'mysql', 'MySQL', '', '2021-05-30 16:48:18', NULL),
(4, 3, 'initiation-mysql', 'Cours initiation MySQL', '', '2021-05-30 16:49:09', NULL);
UPDATE category SET id = 12 WHERE id = 2;
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`cours_cms`.`category`, CONSTRAINT `fk_category_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `category` (`id`) ON UPDATE CASCADE)
查看本页评论:
https://mariadb.com/kb/en/foreign-keys/
If ON UPDATE CASCADE recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE operations. This is to prevent infinite loops resulting from cascaded updates.
换句话说,如果它是 table 的分层数据类型,则 ON UPDATE CASCADE 不起作用。
只是确认这在没有 'infinite loops'
的 PostgreSQL 中有效CREATE TABLE category (
id SERIAL PRIMARY KEY,
parent_id int DEFAULT NULL,
slug varchar(255) NOT NULL,
title varchar(255) NOT NULL,
content text NOT NULL,
CONSTRAINT fk_category_parent_id FOREIGN KEY (parent_id) REFERENCES category (id) ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO category (id, parent_id, slug, title, content) VALUES
(1, NULL, 'non-classe', 'Non Classé', ''),
(2, NULL, 'cours', 'Cours', ''),
(3, 2, 'mysql', 'MySQL', ''),
(4, 3, 'initiation-mysql', 'Cours initiation MySQL', '');
UPDATE category SET id=12 WHERE id=2;
SELECT * FROM category;
id | parent_id | slug | title | content
----+-----------+------------------+------------------------+---------
1 | | non-classe | Non Classé |
4 | 3 | initiation-mysql | Cours initiation MySQL |
12 | | cours | Cours |
3 | 12 | mysql | MySQL |