Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: cannot alter type of a column used by a view or rule DETAIL: rule _RETURN on view depends on column "status"

I am trying to update a column of type integer to numeric(2) in postgres

ALTER TABLE employee_status
    ALTER COLUMN status TYPE numeric(2);

but getting the error ERROR: cannot alter type of a column used by a view or rule DETAIL: rule _RETURN on view v_employee_details depends on column "status"

Without dropping or deleting data, how can i achieve this? is there any way to disable the rule or the view

like image 947
not-a-bug Avatar asked Jul 08 '20 11:07

not-a-bug


People also ask

How do you alter type of a column used by a view or rule?

select util. deps_save_and_drop_dependencies('mdm', 'global_item_master_swap'); alter table mdm. global_item_master_swap alter column prod_id type varchar(128), alter column prod_nme type varchar(512); select util. deps_restore_dependencies('mdm', 'global_item_master_swap');

How do I change the datatype of a PostgreSQL view?

1) Drop the view that is causing the issue, after that run your alter table statement. Then once you altered the table you can create the view again. 2) Or if you cant drop your view, then please follow this particular stackover flow thread=> stackoverflow.com/questions/3243863/…

How do I change the datatype of a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

How do I change the constraint of a column in PostgreSQL?

There is no ALTER command for constraints in Postgres. The easiest way to accomplish this is to drop the constraint and re-add it with the desired parameters. Of course any change of the constraint will be run against the current table data.


2 Answers

The only possible way is to drop and re-create the view.

But that is no problem, you can easily get the view definition with the pg_get_viewdef function.

If you have a lot of dependent views, take a look at this answer to get all dependent views in the correct order.

Don't worry about the _RETURN rule: that is just an implementation detail of how views are implemented in PostgreSQL: as ON SELECT DO INSTEAD rule named _RETURN.


do $$            
  declare v_employee_details_def text;
  declare exec_text text;
begin          
  v_employee_details_def := pg_get_viewdef('v_employee_details');
  drop view v_employee_details;
  
  -- do your other stuff
  
  exec_text := format('create view v_employee_details as %s', 
      v_employee_details_def);
  execute exec_text;
end $$;
like image 181
Laurenz Albe Avatar answered Oct 21 '22 16:10

Laurenz Albe


I found one more solution more simple, but may be not a best practice.

UPDATE pg_attribute SET atttypmod = 35+4 WHERE attrelid = 'TABLE1'::regclass AND attname = 'COL1';

You need a SUPERUSER access or similar to change Postgres system table - pg_attribute. Here's explanation link!

like image 32
Alex Avatar answered Oct 21 '22 17:10

Alex