首页 > 解决方案 > 如何在不使用 CREATE CAST 的情况下创建可以是两种不同数据类型的外键?

问题描述

我有两种类型的表:具有 UUID 主键的表和具有 BIGINT 主键的表。

我有一个特定的表,它引用具有任一类型主键的表。

我不能使用CREATE CAST (character varying AS uuid) WITH INOUT AS IMPLICIT,因为我不拥有character varyinguuid(heroku)。

例如,假设这些表:

CREATE TABLE public.changelogs (
    id bigint NOT NULL,
    record_type character varying NOT NULL,
    -- record_id ??? NOT NULL,
    event character varying NOT NULL,
    actor_id uuid,
    transitions jsonb,
    object_changes jsonb DEFAULT '{}'::jsonb NOT NULL,
    created_at timestamp without time zone NOT NULL,
    group_id text NOT NULL
);

而这两张表:

CREATE TABLE public.recipes (
    id uuid DEFAULT public.gen_random_uuid() NOT NULL,
    name text NOT NULL,
    slug public.citext NOT NULL,
    moderation_state public.citext NOT NULL,
    description text NOT NULL,
    author_id uuid NOT NULL,
    ingredients text[] DEFAULT '{}'::text[] NOT NULL,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL,
    instructions character varying[] DEFAULT '{}'::character varying[] NOT NULL,
    cook_time integer NOT NULL,
    prep_time integer NOT NULL
);

CREATE TABLE public.allergies (
    id bigint NOT NULL,
    name character varying NOT NULL,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL
);

标签: postgresqlheroku-postgres

解决方案


推荐阅读