[英]Is it possible to copy an enum type from one schema to another
I'm using postgREST to generate a http accessible api (great stuff).我正在使用 postgREST 生成一个 http 可访问的 api(很棒的东西)。 There is a little bug that I'm trying to work around.
我正在尝试解决一个小错误。
For whatever reason, when calling a function, and only in the function parameters, I cannot use the types the api
can normally reference using qualified notation eg, core.my_type
.无论出于何种原因,在调用函数时,并且仅在函数参数中,我不能使用
api
通常可以使用限定符号引用的类型,例如core.my_type
。 It can however access api.my_type
where of course I don't need to qualify the type.但是它可以访问
api.my_type
当然我不需要限定类型。
So, to be clear, it all works in postgres.因此,需要明确的是,这一切都适用于 postgres。 It's just a quirk with the postgREST.
这只是 postgREST 的一个怪癖。
One work-around that is prone to error over time, is to copy/paste the definition of each type in each of the schemas.随着时间的推移容易出错的一种解决方法是在每个模式中复制/粘贴每种类型的定义。
The other is the topic of this post: is there a way to automatically create a copy of the type in the core
schema to one in the api
?另一个是这篇文章的主题:有没有办法自动将
core
模式中的类型复制到api
中的类型? Or, is there a way to reference the core.my_type
using an alias?或者,有没有办法使用别名引用
core.my_type
? (not sure if the latter would solve the problem, but perhaps worth a try). (不确定后者是否能解决问题,但也许值得一试)。
I realize it would require casting where required.我意识到它需要在需要的地方进行铸造。 However, it does solve the problem of tracking the entries in each of the
enums
(in this case).但是,它确实解决了跟踪每个
enums
中的条目的问题(在这种情况下)。
For whatever reason, when calling a function, and only in the function parameters, I cannot use the types the api can normally reference using qualified notation
无论出于何种原因,在调用函数时,并且仅在函数参数中,我不能使用 api 通常可以使用限定符号引用的类型
That is because PostgREST uses a CTE when building the query to call the function and casts the data to the types of the parameters as seen in these lines of code .这是因为 PostgREST 在构建查询时使用 CTE 来调用函数并将数据转换为在这些代码行中看到的参数类型。
There is a closed issue in the GitHub repository mentioning this problem that is labeled as won't fix
. GitHub 存储库中有一个已关闭的问题,提到了这个标记为
won't fix
的问题。
The other is the topic of this post: is there a way to automatically create a copy of the type in the core schema to one in the api?
另一个是这篇文章的主题:有没有办法自动将核心模式中的类型复制到api中的一个? Or, is there a way to reference the core.my_type using an alias?
或者,有没有办法使用别名引用 core.my_type ?
You could create a DOMAIN
as a workaround.您可以创建一个
DOMAIN
作为解决方法。 That way, any modification you do using ALTER
on the underlying private data type will be reflected on the domain.这样,您对底层私有数据类型使用
ALTER
所做的任何修改都将反映在域上。 For instance:例如:
create schema api;
create schema private;
create role web_anon nologin;
-- web_anon doesn't have usage on the private schema
grant usage on schema api to web_anon;
-- create type and domain
create type private.grade as enum('a','b','c','d','e');
create domain api.grade as private.grade;
-- The function uses the domain api.grade instead of private.grade
create or replace function
api.get_grade(g api.grade) returns text as $$
begin
return (select g);
end;
$$ language plpgsql;
-- This change will reflect on the domain
alter type private.grade add value 'f';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.