python
config_error
ai_generated
true
列 'tags' 的可变默认值 <list object at 0x...> 不允许。请使用可调用对象。
sqlalchemy.exc.ArgumentError: Mutable default <list object at 0x...> for column 'tags' is not allowed. Use a callable instead.
ID: python/sqlalchemy-mutable-default-list
80%修复率
90%置信度
0证据数
2025-07-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在 Column 中使用可变对象(如列表或字典)作为默认值,可能导致实例间共享状态。
English
Using a mutable object (like list or dict) as a default value in a Column, which can lead to shared state across instances.
解决方案
-
95% 成功率
class Article(Base): __tablename__ = 'articles' tags = Column(JSON, default=list) # or default=lambda: []
无效尝试
常见但无效的做法:
-
30% 失败
Lambda is not serializable and may cause issues with migrations.
-
50% 失败
Tuple is immutable but may not be suitable for all use cases.