postgresql - 如何在不使用 CREATE CAST 的情况下创建可以是两种不同数据类型的外键?
问题描述
我有两种类型的表:具有 UUID 主键的表和具有 BIGINT 主键的表。
我有一个特定的表,它引用具有任一类型主键的表。
我不能使用CREATE CAST (character varying AS uuid) WITH INOUT AS IMPLICIT
,因为我不拥有character varying
或uuid
(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
);
解决方案
推荐阅读
- sql-server - SQL server TVP批量插入超过2000个参数
- python - Matplotlib - 对数图表 - 向图表添加额外文本
- reactjs - React.js - 在 Netlify 或 Firebase 上部署站点 - 缺少 CSS 文件
- laravel - 自定义 Laravel Fortify 注册错误
- sql-server - 第 1 行第 28 列的批量加载数据转换错误(截断)
- r - 如何为未找到的 xpath 返回 NA
- python - TypeError:期望字符串或字节格式的键
- r - DuckDB 不会在免费的 AWS EC2 实例上编译。预编译包是解决方案吗?
- javascript - 如何从 fastapi websocket 答案中获取“lastEventId”?
- postgresql - 如何找到运行 psql 查询的脚本?