[Chore/Refactor] Fix SQLAlchemy deprecation warnings for callable defaults in ORM dataclasses #20584

Closed
opened 2026-02-21 20:08:03 -05:00 by yindo · 0 comments
Owner

Originally created by @laipz8200 on GitHub (Nov 26, 2025).

Originally assigned to: @laipz8200 on GitHub.

Description

Several ORM model classes use default=lambda: ... for the id field with init=False. SQLAlchemy now raises deprecation warnings for this pattern in ORM-mapped Dataclasses context, as the behavior is ambiguous.

The affected models are in:

  • api/models/account.py: Account, Tenant, TenantAccountJoin, AccountIntegrate
  • api/models/trigger.py: TriggerSubscription, TriggerOAuthSystemClient, TriggerOAuthTenantClient, WorkflowTriggerLog, WorkflowWebhookTrigger, WorkflowPluginTrigger, AppTrigger, WorkflowSchedulePlan
  • api/models/web.py: SavedMessage, PinnedConversation
  • api/models/workflow.py: WorkflowAppLog

The fix is to change default= to default_factory= for fields that have init=False, which clearly indicates that the callable provides default values for the dataclass instance.

Motivation

This change eliminates SQLAlchemy deprecation warnings that appear during test runs and application startup. These warnings indicate that the current usage pattern will raise errors in future SQLAlchemy releases, so addressing them now ensures forward compatibility.

=============================== warnings summary ===============================
api/models/account.py:87
  /home/runner/work/dify/dify/api/models/account.py:87: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class Account(UserMixin, TypeBase):

api/models/account.py:234
  /home/runner/work/dify/dify/api/models/account.py:234: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class Tenant(TypeBase):

api/models/account.py:269
  /home/runner/work/dify/dify/api/models/account.py:269: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class TenantAccountJoin(TypeBase):

api/models/account.py:292
  /home/runner/work/dify/dify/api/models/account.py:292: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class AccountIntegrate(TypeBase):

api/models/account.py:334
  /home/runner/work/dify/dify/api/models/account.py:334: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class TenantPluginPermission(TypeBase):

api/models/account.py:361
  /home/runner/work/dify/dify/api/models/account.py:361: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class TenantPluginAutoUpgradeStrategy(TypeBase):

api/models/api_based_extension.py:20
  /home/runner/work/dify/dify/api/models/api_based_extension.py:20: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release.  If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead.  To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter.
    class APIBasedExtension(TypeBase):

......

https://github.com/langgenius/dify/actions/runs/19713317650/job/56479409097?pr=27771

Originally created by @laipz8200 on GitHub (Nov 26, 2025). Originally assigned to: @laipz8200 on GitHub. ## Description Several ORM model classes use `default=lambda: ...` for the `id` field with `init=False`. SQLAlchemy now raises deprecation warnings for this pattern in ORM-mapped Dataclasses context, as the behavior is ambiguous. The affected models are in: - `api/models/account.py`: `Account`, `Tenant`, `TenantAccountJoin`, `AccountIntegrate` - `api/models/trigger.py`: `TriggerSubscription`, `TriggerOAuthSystemClient`, `TriggerOAuthTenantClient`, `WorkflowTriggerLog`, `WorkflowWebhookTrigger`, `WorkflowPluginTrigger`, `AppTrigger`, `WorkflowSchedulePlan` - `api/models/web.py`: `SavedMessage`, `PinnedConversation` - `api/models/workflow.py`: `WorkflowAppLog` The fix is to change `default=` to `default_factory=` for fields that have `init=False`, which clearly indicates that the callable provides default values for the dataclass instance. ## Motivation This change eliminates SQLAlchemy deprecation warnings that appear during test runs and application startup. These warnings indicate that the current usage pattern will raise errors in future SQLAlchemy releases, so addressing them now ensures forward compatibility. ``` =============================== warnings summary =============================== api/models/account.py:87 /home/runner/work/dify/dify/api/models/account.py:87: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class Account(UserMixin, TypeBase): api/models/account.py:234 /home/runner/work/dify/dify/api/models/account.py:234: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class Tenant(TypeBase): api/models/account.py:269 /home/runner/work/dify/dify/api/models/account.py:269: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class TenantAccountJoin(TypeBase): api/models/account.py:292 /home/runner/work/dify/dify/api/models/account.py:292: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class AccountIntegrate(TypeBase): api/models/account.py:334 /home/runner/work/dify/dify/api/models/account.py:334: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class TenantPluginPermission(TypeBase): api/models/account.py:361 /home/runner/work/dify/dify/api/models/account.py:361: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class TenantPluginAutoUpgradeStrategy(TypeBase): api/models/api_based_extension.py:20 /home/runner/work/dify/dify/api/models/api_based_extension.py:20: SADeprecationWarning: Callable object passed to the ``default`` parameter for attribute 'id' in a ORM-mapped Dataclasses context is ambiguous, and this use will raise an error in a future release. If this callable is intended to produce Core level INSERT default values for an underlying ``Column``, use the ``mapped_column.insert_default`` parameter instead. To establish this callable as providing a default value for instances of the dataclass itself, use the ``default_factory`` dataclasses parameter. class APIBasedExtension(TypeBase): ...... ``` https://github.com/langgenius/dify/actions/runs/19713317650/job/56479409097?pr=27771
yindo closed this issue 2026-02-21 20:08:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#20584