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
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');
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/…
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.
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.
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 $$;
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With