gpt4 book ai didi

python - 无效请求错误 : Object '' is already attached to session

转载 作者:行者123 更新时间:2023-11-28 16:38:54 30 4
gpt4 key购买 nike

我得到:

sqlalchemy.exc.InvalidRequestError: Object '<DBEdge at 0x103c4a190>' is already attached to session '1' (this is '2')

在这一行

session.add(edge)

当我尝试运行它时:

  def findOrCreateEdge(self,user1,user2): #user1 is followed by user2
if user1.id>user2.id:
user1, user2 = user2, user1
kind = 2
else:
kind = 1
edge = self.findEdge(user1,user2)
if edge:
if edge.kind==1 and kind==2:
edge.kind = 3
if edge.kind==2 and kind==1:
edge.kind = 3
return edge
else:
edge = DBEdge(user1,user2)
edge.kind = kind
user1.edge_count = user1.edge_count + 1
user2.edge_count = user2.edge_count + 1
#session.save(edge) # we don't flush edges here, because it gives us a huge speedup to do a bunch at once then flush.
try:
session.add(edge)
session.commit()
except:
session = Session()
session.add(edge)
session.commit()
return edge

最佳答案

问题在于两次添加边缘,一次添加到第一个 session ,然后在出现异常时添加到第二个 session 。我不知道你到底想在这里实现什么,但如果你调查错误并解决问题,而不是粗暴地将边缘塞入新 session ,会更好。或者,您可能希望回滚之前的 session 并重新启动它,然后向其添加边缘。

在已有 session 的情况下创建新 session 几乎总是错误的(除非上下文本身发生了变化,但这里似乎不是这种情况)。

关于python - 无效请求错误 : Object '' is already attached to session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22110757/

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