当尝试使用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"}
非常感谢任何帮助。谢谢你。