sql - 向 PostgreSQL 插入和更新数据的一些问题
问题描述
我的任务是将数据从一个模式复制到 PostgreSQL 中同一数据库的另一个模式:
Insert :
Load table incrementally using project_id as a lookup. Insert project record into this table when project_id does not exist.
Update :
If project_id already exists and the value of other attributes is changed, then update only the changed values for the collect project (project_id).
我不太明白请求应该是什么。我的基本是这样的:
INSERT INTO target_schema.supplies select * from source_schema.supplies
但有一个错误:
2021-08-10 13:05:23,482] {{taskinstance.py:1128}} ERROR - duplicate key value violates unique constraint "supplies_pkey"
DETAIL: Key (id)=(2) already exists.
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 966, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/lib/python3.7/site-packages/airflow/operators/python_operator.py", line 113, in execute
return_value = self.execute_callable()
File "/usr/local/lib/python3.7/site-packages/airflow/operators/python_operator.py", line 118, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
File "/usr/local/airflow/dags/local_copy_source_schema_to_target_schema.py", line 31, in copy_data_from_source_schema_to_target_schema
redshift_hook.run(sql_queries)
File "/usr/local/lib/python3.7/site-packages/airflow/hooks/dbapi_hook.py", line 175, in run
cur.execute(s)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "supplies_pkey"
DETAIL: Key (id)=(2) already exists.
以及未满足任务的其他条件。
解决方案
您应该尝试 ON CONFLICT。
INSERT INTO users (id, level)
VALUES (1, 0)
ON CONFLICT (id) DO UPDATE
SET level = users.level + 1;
取自SO
推荐阅读
- infinite-loop - 霍尔逻辑 | 当存在无限循环时,什么后置条件有效?
- discord.js - 当有人加入或离开其语音频道时,让机器人向文本频道发送消息
- php - 在 laravel 获取数据中没有得到想要的结果
- excel - VBA Web Scraper - 网页在输入新参数后恢复到第一个搜索参数
- shell - Applescripts:将击键发送到通过 shell 脚本调用的 java 程序
- python-3.x - 带有 ValueError 的日期时间转换
- c - CS50 为什么我的密码无法破解四位密码?
- python - 在 Python 中重复代码以进行颜色更改程序
- java - 如何将临时地图合并到永久地图上,以便记录相同密钥的多个项目?
- rust - 如何在 rust 中构建多工作空间货物项目