Viewを更新でエラー(Postgres)

Postgresを利用していて、Viewにカラム追加が必要になった際、CREATE OR REPLACE VIEWを発行して、エラーが発生しました。

構文としては特におかしな箇所もなさそうなのですが、
cannot change name of “OldColumn” to “NewColumn”
といった感じのエラーが発生しました。

カラムのリネームをしているわけでもないのに変だな、と思い調べてみるとどうやら以下に書いてあることが原因だそう。

CAUTION:

The CREATE OR REPLACE VIEW statement will work if you are adding columns to the view at the end of the list. However, it will error if you are adding new columns within the existing columns (ie: start or middle of the existing list).

In this case, do not use the CREATE OR REPLACE VIEW statement. It is better to drop the view and use the CREATE VIEW statement!

リストの最後にカラムを追加する場合はOKだが、リストの途中に追加する場合にはエラーとなります。このような場合はCREATE OR REPLACEを使わずに、VIEWをDROPしてからCREATE VIEWを使ってください、と書いてます。

これってPostgresだけの事象なんですかね?
深くは調べてないですが、VIEWが更新できない時はDROPする方が確実かもしれませんね。

PostgreSQL: VIEW
ThisPostgreSQLtutorialexplainshowtocreate,update,anddropVIEWSinPostgreSQLwithsyntaxandexamples.InPostgreSQL,aVIEWisnotaphysicaltable,butrather,itisinessenceavir...

コメント