简体   繁体   English

是否可以将枚举类型从一个模式复制到另一个模式

[英]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.

相关问题 是否可以在PostgreSQL中的另一个模式中使用一个模式的枚举类型 - is it possible to use enum type of one schema in another schema in postgresql 是否可以在 PostgreSQL 中从一个枚举转换为另一个枚举 - Is it possible to cast from one enum to another in PostgreSQL Rails - 将表从一个模式复制到同一数据库中的另一个模式 - Rails - copy a Table from one schema to another schema in same database 如何将枚举值从一个枚举列“复制”到另一个枚举列? - How do I “copy” enum values from one enum column to another enum column? Postgres 11 - 如何将函数从一个模式复制到另一个模式 - Postgres 11 - how to copy functions from one schema to another 如何在 Postgresql 中将序列从一个模式复制到另一个模式? - How can I copy sequences from one schema to another in Postgresql? 如何在保留原始模式的 Postgres 中将某些表从一个模式复制到另一个模式? - How to copy certain tables from one schema to another within same DB in Postgres keeping the original schema? 我们如何将外部表从一个模式复制到同一 postgresql 数据库中的另一个模式 - how do we copy external tables from one schema to another schema in same postgresql database 另一个模式中枚举的 jOOQ 代码生成器 - jOOQ codegen for enum in another schema 是否可以使用postgres COPY将选择从一个表输出到另一个文本字段作为csv - Is it possible to use postgres COPY to make a select output from one table to another text field as csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM