在 postgresql 中将列从字符串更改为字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14782148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:47:31  来源:igfitidea点击:

Changing a column from string to string array in postgresql

arraysdatabasepostgresqlpostgresql-9.2

提问by papdel

The following is a snippet of a table called "containers".

以下是名为“容器”的表的片段。

       Column       |            Type             |            Modifiers            
--------------------+-----------------------------+---------------------------------
 id                 | uuid                        | not null
 name               | character varying(255)      | 
 products           | character varying           | default '{}'::character varying

How can I alter the productscolumn to "character varying[]"and the corresponding modifiers to default '{}'::character varying[]? Essentially, I want to convert a string to a string array. Note the products column has no limit on the number of characters.

如何将products列更改为"character varying[]"并将相应的修饰符更改为default '{}'::character varying[]?本质上,我想将字符串转换为字符串数组。请注意,产品列对字符数没有限制。

alter table "containers" alter "products" type character varying[];

throws the following error

抛出以下错误

ERROR: column "products" cannot be cast to type character varying[]

错误:列“产品”不能转换为类型字符变化[]

回答by klin

There is no implicit cast from varcharto varchar[]in Postgres. You must indicate how to perform the conversion of the types. You should do it in USING expressionclause (see ALTER TABLEin the documentation). In that case you have to drop and recreate the default value of the column, as it is explained in the documentation:

Postgres 中没有从varcharto 的隐式转换varchar[]。您必须指明如何执行类型转换。您应该在USING expression子句中执行此操作(请参阅文档中的ALTER TABLE)。在这种情况下,您必须删除并重新创建列的默认值,如文档中所述:

The USING option of SET DATA TYPE can actually specify any expression involving the old values of the row; that is, it can refer to other columns as well as the one being converted. This allows very general conversions to be done with the SET DATA TYPE syntax. Because of this flexibility, the USING expression is not applied to the column's default value (if any); the result might not be a constant expression as required for a default. This means that when there is no implicit or assignment cast from old to new type, SET DATA TYPE might fail to convert the default even though a USING clause is supplied. In such cases, drop the default with DROP DEFAULT, perform the ALTER TYPE, and then use SET DEFAULT to add a suitable new default.

SET DATA TYPE 的 USING 选项实际上可以指定任何涉及行旧值的表达式;也就是说,它可以引用其他列以及被转换的列。这允许使用 SET DATA TYPE 语法完成非常通用的转换。由于这种灵活性,USING 表达式不会应用于列的默认值(如果有);结果可能不是默认值所需的常量表达式。这意味着当没有隐式或赋值从旧类型转换为新类型时,即使提供了 USING 子句,SET DATA TYPE 也可能无法转换默认值。在这种情况下,使用 DROP DEFAULT 删除默认值,执行 ALTER TYPE,然后使用 SET DEFAULT 添加合适的新默认值。

alter table containers alter products drop default;
alter table containers alter products type text[] using array[products];
alter table containers alter products set default '{}';

The three operations can be done in one statement:

这三个操作可以在一个语句中完成:

alter table containers 
    alter products drop default,
    alter products type text[] using array[products],
    alter products set default '{}';