在postgresql中将列从字符串更改为字符串数组 [英] Changing a column from string to string array in postgresql

查看:203
本文介绍了在postgresql中将列从字符串更改为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个名为containers的表的片段。

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

如何修改 变化字符[]和相应的修饰符 ?基本上,我想将一个字符串转换为字符串数组。注意,products列对字符数没有限制。

How can I alter the products column 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.

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

会引发以下错误


错误:栏products不能转换为类型字符变化[]

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


推荐答案

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

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


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

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.



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 '{}';

这篇关于在postgresql中将列从字符串更改为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆