Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres change data type of view column from unknown to text

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.

like image 344
Angga Saputra Avatar asked May 16 '13 11:05

Angga Saputra


People also ask

How can you change the data type for a particular column?

Change data types in Design viewLocate the field that you want to change, and select a new data type from the list in the Data Type column. Save your changes.

Can we alter column in view?

The altered view replaces the existing view, so you cannot modify specific columns in a view. A view is a virtual table based on the result set of a SELECT query or a UNION of such queries. For more details on views, see Defining and Using Views.

How do I change the view in PostgreSQL?

ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT ALTER VIEW [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ALTER VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO ...


1 Answers

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.

like image 169
Mike Sherrill 'Cat Recall' Avatar answered Sep 20 '22 09:09

Mike Sherrill 'Cat Recall'