简体   繁体   English

空数组作为 PostgreSQL 数组列默认值

[英]Empty array as PostgreSQL array column default value

I have a defined an array field in postgresql 9.4 database:我在 postgresql 9.4 数据库中定义了一个数组字段:

character varying(64)[]

Can I have an empty array eg {} for default value of that field?我可以有一个空数组,例如 {} 作为该字段的默认值吗? What will be the syntax for setting so?设置的语法是什么?

I'm getting following error in case of setting just brackets {}:如果只设置括号 {},我会收到以下错误:

SQL error:

ERROR:  syntax error at or near "{"
LINE 1: ...public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}
                                                                     ^

In statement:
ALTER TABLE "public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}

You need to use the explicit array initializer and cast that to the correct type:您需要使用显式array初始值设定项并将其转换为正确的类型:

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history SET DEFAULT array[]::varchar[];

I tested both the accepted answer and the one from the comments.我测试了接受的答案和评论中的答案。 They both work.他们都工作。
I'll graduate the comments to an answer as it's my preferred syntax.我会将评论归结为答案,因为这是我的首选语法。 🙂 🙂

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history SET DEFAULT '{}';

It threw an error where it can not find SET .它在找不到SET地方抛出了一个错误。 This worked for me.这对我有用。

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history DEFAULT array[]::varchar[];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM