python
runtime_error
ai_generated
true
sqlalchemy.orm.exc.ConcurrentModificationError: Object '<User at 0x...>' is being merged into a Session but the same instance is already present
ID: python/sqlalchemy-orm-exc-concurrent-modification-error
80%Fix Rate
82%Confidence
0Evidence
2024-11-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Attempting to merge an ORM instance that is already tracked by the same session, causing a conflict.
generic中文
尝试合并一个已被同一会话跟踪的 ORM 实例,导致冲突。
Workarounds
-
80% success Check session identity map
Check if the instance is already in the session using session.is_modified() or track identities manually before merging.
-
90% success Merge with fresh instance
Use session.merge() with a new instance that has the same primary key, ensuring the session does not already contain it.
Dead Ends
Common approaches that don't work:
- Using add() instead of merge() 70% fail
- Expunging the instance first 50% fail