python config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2025-07-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Using a mutable object (like list or dict) as a default value in a Column, which can lead to shared state across instances.

generic

中文

在 Column 中使用可变对象(如列表或字典)作为默认值,可能导致实例间共享状态。

Workarounds

  1. 95% success
    class Article(Base):
        __tablename__ = 'articles'
        tags = Column(JSON, default=list)  # or default=lambda: []

Dead Ends

Common approaches that don't work:

  1. 30% fail

    Lambda is not serializable and may cause issues with migrations.

  2. 50% fail

    Tuple is immutable but may not be suitable for all use cases.