在 ON CONFLICT 中将行列从一个 table 复制到另一个

Copy a row column from one table to another in ON CONFLICT

使用 Postgres 9.6,我将一行(选定的列)从一个 table 复制到另一个。我是 运行:

INSERT INTO copy_on_write(id, images)
    SELECT id, images
    FROM listings
    WHERE id = 1 AND posted_by = foo
ON CONFLICT (id) DO UPDATE SET images = listings.images 

由于 id 已经存在而发生冲突,我得到 missing FROM-clause entry for table "listings"

所以,我尝试了:

INSERT INTO copy_on_write(id, images)
    SELECT id, images
    FROM listings
    WHERE id = 1 AND posted_by = foo
ON CONFLICT (id) DO UPDATE SET images = images FROM listings

但后来我得到 syntax error at or near "FROM"

我如何处理这个 ON CONFLICT (id) 以便在行 ID 已经存在时更新图像列?

使用excluded:

INSERT INTO copy_on_write(id, images) 
SELECT id, images 
FROM listings 
WHERE id = 1 AND posted_by = 'foo' 
ON CONFLICT (id) DO UPDATE SET images = excluded.images;

来自the documentation:

conflict_action

(...) The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the special excluded table.