【发布时间】:2020-03-08 17:53:38
【问题描述】:
当我尝试使用 pg8000 加载到 postgresql 中的表时,我正在尝试运行一个测试用例来捕获主键异常。但是,我无法在 python 中捕捉到它。下面是我的代码。
import pg8000
def create_albums(self,music):
experts = []
exp = music.data_info.get("artist_list") # List of a dictionary
try:
for expert in exp:
experts.append(ArtistData({
"name": expert.name,
"age": expert.age,
"present": True,
"nodiscs": expert.get("disc_code")
}))
except pg8000.core.IntegrityError:
raise Exception
return experts
下面是错误信息
pg8000.core.IntegrityError: {'S': 'ERROR', 'V': 'ERROR', 'C': '23505', 'M': 'duplicate key value violates unique constraint "artist_discs_pkey"}
非常感谢任何帮助。谢谢。
【问题讨论】:
-
运行代码时究竟发生了什么?
-
所以(主键)PK 是名称和 disc_code。当我有相同的名字和相同的 disc_code。它发出“重复键值违反唯一约束”。
标签: python postgresql pg8000