postgres change data type of view column from unknown to text

Angga Saputra picture Angga Saputra · May 16, 2013 · Viewed 11.2k times · Source

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

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

'pointrewarding'::text

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

CAST('pointrewarding' AS TEXT)

How to solve this problem.

Answer

Mike Sherrill 'Cat Recall' picture Mike Sherrill 'Cat Recall' · May 16, 2013

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

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 names in the same order and with the same data types), but it may add additional columns to the end of the list.

Emphasis added.