我有一个SQL表,它可以引用表中的另一个记录作为其parent
,但不应该引用它自己.我曾try 使用CHECK
约束来强制执行此操作,但try 失败了,因为id
是一个自动递增列.有没有其他方法可以确保parent_id <> id
?
我当前的try 失败,错误为Check constraint 'not_own_parent' cannot refer to an auto-increment column. (errno 3818)
:
CREATE TABLE `content` (
`id` serial PRIMARY KEY NOT NULL,
`item_id` int NOT NULL,
`nested_item_id` int,
`block_id` int,
`order` int NOT NULL,
CONSTRAINT not_own_parent CHECK (nested_item_id <> id)
);