给定 3 个表(例如posts
、comments
和tags
),有没有办法让其中一个表的外键引用另外两个表之一的主键?
CREATE TABLE IF NOT EXISTS posts (
id UUID DEFAULT uuid_generate_v4 ()
CONSTRAINT posts_pk PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS comments (
id UUID DEFAULT uuid_generate_v4 ()
CONSTRAINT comments_pk PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS tags (
id UUID DEFAULT uuid_generate_v4 ()
CONSTRAINT tags_pk PRIMARY KEY
);
DROP TYPE IF EXISTS s_type;
CREATE TYPE s_type AS ENUM (
'POST',
'COMMENT'
);
CREATE TABLE IF NOT EXISTS tag_mentions (
id UUID DEFAULT uuid_generate_v4 ()
CONSTRAINT tag_mentions_pk PRIMARY KEY,
source_type s_type NOT NULL,
source_id UUID NOT NULL
CONSTRAINT tag_mention_id_fk
-- Is something like this possible?
REFERENCES posts (id) OR comments (id)
);
s_type
也许有一种方法可以通过和posts
和ID之间的复合索引来做到这一点comments
?