postgresql postgres 将视图列的数据类型从未知更改为文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16586215/
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:54:59  来源:igfitidea点击:

postgres change data type of view column from unknown to text

postgresqlviewsqldatatypes

提问by Angga Saputra

I just create a new view as follows

我只是创建一个新视图如下

CREATE OR REPLACE VIEW gettreelistvw AS 
 SELECT "CRM".groupid, 'pointrewarding'::text AS applicationid, "CM".menuid, "CM".menuname, "CM".levelstructure, "CM".moduleid, "CM".haschild, "CM".installed
   FROM core_capabilitymap "CRM"
   JOIN core_menus "CM" ON "CRM".menuid::text = "CM".menuid::text;

ALTER TABLE gettreelistvw

when i execute this error appear

当我执行此错误时出现

ERROR: cannot change data type of view column "applicationid" from unknown to text

错误:无法将视图列“applicationid”的数据类型从未知更改为文本

although I already cast the value of applicationid column to text. it's still recognized as unkown datatype

虽然我已经将 applicationid 列的值转换为文本。它仍然被认为是未知的数据类型

'pointrewarding'::text

The alternative method of postgres conversion also didn't work.

postgres 转换的替代方法也不起作用。

CAST('pointrewarding' AS TEXT)

How to solve this problem.

如何解决这个问题呢。

回答by Mike Sherrill 'Cat Recall'

If you want to change the data type of a view's columns, you have to drop it, then create it.

如果要更改视图列的数据类型,必须先删除它,然后再创建它。

Version 9.2 docs

版本 9.2 文档

CREATE OR REPLACE VIEW .... The new query must generate the same columns that were generated by the existing view query (that is, the same column namesin the same orderand with the same data types), but it may add additional columns to the end of the list.

CREATE OR REPLACE VIEW .... 新查询必须生成与现有视图查询生成的列相同的列(即,相同的列名相同的顺序相同的数据类型),但它可能会添加额外的列到列表的末尾。

Emphasis added.

强调了。