【发布时间】:2021-09-07 23:13:18
【问题描述】:
我正在尝试使用 PeeWee 根据另一个表中的值 UPSERT 进入 postgres 数据库。
**table1**
pk_t1 int
name
city
country
**table2**
pk_t2 int
name
city
country
comments
INSERT INTO table2 (pk_t2, name, city, country)
SELECT pk_1, name, city, country
FROM table1
ON CONFLICT (pk_t2) DO UPDATE
SET name = excluded.name, city = excluded.city, country = excluded.country;
但我无法从文档或 SO 中找到合适的 peewee 示例。
【问题讨论】:
-
您可以在 UPDATE 查询中指定 FROM 子句:docs.peewee-orm.com/en/latest/peewee/api.html#Update.from_ -- 请参阅文档以获取示例并进行实验,直到您得到想要的结果。
-
@coleifer 我尝试使用更新查询。它只更新现有记录。我正在尝试插入(如果记录不存在)和更新(如果记录存在)。
标签: postgresql peewee