PostgreSQL复制带有生成列的表中的行

pbpqsu0x  于 2022-12-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(107)

Let's say I have a table with generated column.

CREATE TABLE foo (x int, y int, z int GENERATED ALWAYS AS (x + y) STORED);
INSERT INTO foo (x, y) VALUES (1, 1);

Is it possible to do something like this, but somehow omitting the generated column?

INSERT INTO foo SELECT * FROM foo;

I don't want to specify all the column names, because I want the insert to work even if new columns are added to the table.
I couldn't find any clever solution. this question provides some idea, but I don't know whether it could be adapted. The only way I could think of is to set z value in before trigger instead of using generated column. Are there any recommendations in this area?

mnemlml8

mnemlml81#

我建议在几乎所有情况下都避免使用隐式列。在INSERT和SELECT中指定所需的列:

INSERT INTO foo (x, y) SELECT x, y FROM foo;

在编程中,显式是一件好事。

  • 在所有条件相同的情况下,最简单的解决方案往往是最好的 *

相关问题