如何创建一个不会阻止更改其基础表的视图?每次我需要更改列类型或添加新列时,我都无法删除并重新创建视图,这是有一定原因的。这是因为当我们更改复杂类型定义时,我们的 ORM 会生成 alter 脚本,并且我不想更改稳定的部署过程。
-- test initialization
drop view if exists test_dep_trk_v;
drop table if exists test_dep_trk;
create table test_dep_trk ( a varchar(10) );
-- the view MUST at all times contain all the columns of the table
create view test_dep_trk_v as select * from test_dep_trk;
-- this statement cannot be changed
alter table test_dep_trk alter column a type varchar(11);
.
ERROR: cannot alter type of a column used by a view or rule
DETAIL: rule _RETURN on view test_dep_trk_v depends on column "a"
SQL state: 0A000
看起来这可以通过 9.4 的事件触发器来完成。有一些弱点:
pg_trigger_depth()
总是返回零(这是一个错误吗?)。这就是为什么我必须使用非临时表来防止递归。current_query()
即使我们是从存储过程中调用的,也会返回客户端的语句。_