gpt4 book ai didi

python - AttributeError : 'IntegrityError' object has no attribute 'message' SQLAlchemy

转载 作者:行者123 更新时间:2023-12-03 07:37:41 27 4
gpt4 key购买 nike

我正在研究一个有关使用try/except块在SQLAlchemy Core中捕获异常的书籍示例:

from sqlalchemy.exc import IntegrityError
ins = insert(users).values(
username="cookiemon", #This username is already in db,
email_address="damon@cookie.com", should throw an IntegrityError
phone="111-111-1111", for violating unique constraint
password="password"
)
try:
result=connection.execute(ins)
except IntegrityError as error:
print(error.orig.message, error.params)

但是,我收到以下错误消息:

AttributeError: 'IntegrityError' object has no attribute 'message'



这是本书示例的逐字记录,为什么我不能显示错误消息?

最佳答案

在Python3中,异常不再具有message属性,它们具有args。参见python.org/dev/peps/pep-0352/#transition-plan

Ilja Everilä记入this comment

关于python - AttributeError : 'IntegrityError' object has no attribute 'message' SQLAlchemy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com