python
runtime_error
ai_generated
true
sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x...> is detached; merge() cannot proceed because the session already has a different instance with the same primary key
ID: python/sqlalchemy-orm-exc-detached-instance-merge-conflict
80%Fix Rate
82%Confidence
0Evidence
2025-09-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Attempting to merge a detached instance when the session already tracks another instance with the same primary key, causing identity conflict.
generic中文
尝试合并一个分离的实例,但会话已跟踪另一个具有相同主键的实例,导致身份冲突。
Workarounds
-
85% success Expunge existing instance first
Expunge the existing instance from session before merge: session.expunge(existing_instance); session.merge(detached_instance)
-
90% success Merge with clean session
Use session.merge() with a new instance that does not conflict, or ensure the session is clean before merging.
Dead Ends
Common approaches that don't work:
- Deleting existing instance 60% fail
- Using add() instead 70% fail