This error occurs when you try to attach an instance to a session in SQLAlchemy, but another instance with the same key is already present in the session.

In your case, it seems like you are trying to attach an instance of the HandleDuidian class to a session. However, there is already another instance of HandleDuidian with the key (188464,) present in the session.

To resolve this issue, you can either remove the existing instance from the session before attaching the new one, or update the existing instance instead of attaching a new one. Here's an example of how you can remove the existing instance from the session:

from sqlalchemy.orm import sessionmaker

Session = sessionmaker(bind=engine)
session = Session()

existing_instance = session.query(HandleDuidian).get((188464,))
session.delete(existing_instance)

new_instance = HandleDuidian(...)
session.add(new_instance)

session.commit()

In this example, we first query the existing instance with the key (188464,) from the session using session.query(HandleDuidian).get((188464,)). We then use session.delete() to remove the existing instance from the session.

After that, you can attach the new instance to the session using session.add() and commit the changes with session.commit()

sqlalchemyexcInvalidRequestError Cant attach instance HandleDuidian at 0x25ec4c04dc8; another instance with key class apptoolListmodelHandleDuidian 188464 None is already present in this session

原文地址: https://www.cveoy.top/t/topic/idsu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录