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

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

问题描述

以下是名为容器"的表的片段.

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 列更改为 "character changed[]" 并将相应的修饰符更改为 default '{}'::character changed[] ?本质上,我想将字符串转换为字符串数组.注意产品栏没有字符数限制.

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 中没有从 varcharvarchar[] 的隐式转换.您必须指明如何执行类型转换.您应该在 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天全站免登陆