can't save webapp settings #8871

Closed
opened 2026-02-21 18:28:16 -05:00 by yindo · 3 comments
Owner

Originally created by @hjlarry on GitHub (Mar 2, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

newest main branch

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

Image

when click save button, raise http 500

the log:

2025-03-03 02:12:42,627 ERROR [app.py:875] fdb8189d46 Exception on /console/api/apps/736f27a7-26d0-434a-9443-d341294874b1/site [POST]
Traceback (most recent call last):
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 489, in wrapper
    resp = resource(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/views.py", line 110, in view
    return current_app.ensure_sync(self.dispatch_request)(**kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 604, in dispatch_request
    resp = meth(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/controllers/console/wraps.py", line 147, in decorated
    return view(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/libs/login.py", line 94, in decorated_view
    return current_app.ensure_sync(func)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/controllers/console/wraps.py", line 27, in decorated
    return view(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/controllers/console/app/wraps.py", line 48, in decorated_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 701, in wrapper
    return marshal(resp, self.fields, self.envelope)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 657, in marshal
    return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)
                                                                          ^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 655, in <genexpr>
    else make(v).output(k, data))
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 118, in output
    value = get_value(key if self.attribute is None else self.attribute, obj)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 40, in get_value
    return _get_value_for_keys(key.split('.'), obj, default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 45, in _get_value_for_keys
    return _get_value_for_key(keys[0], obj, default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 57, in _get_value_for_key
    return getattr(obj, key, default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__
    return self.impl.get(state, dict_)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get
    value = self._fire_loader_callables(state, key, passive)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables
    return state._load_expired(state, passive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired
    self.manager.expired_attribute_loader(self, toload, passive)
  File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes
    raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Instance <Site at 0x17075f4d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
2025-03-03 02:12:42,629 INFO [_internal.py:97]  127.0.0.1 - - [03/Mar/2025 02:12:42] "POST /console/api/apps/736f27a7-26d0-434a-9443-d341294874b1/site HTTP/1.1" 500 -

✔️ Expected Behavior

No response

Actual Behavior

No response

Originally created by @hjlarry on GitHub (Mar 2, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version newest main branch ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce ![Image](https://github.com/user-attachments/assets/d05b848b-5fca-4181-8458-48a99cd83649) when click save button, raise http 500 the log: ``` 2025-03-03 02:12:42,627 ERROR [app.py:875] fdb8189d46 Exception on /console/api/apps/736f27a7-26d0-434a-9443-d341294874b1/site [POST] Traceback (most recent call last): File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 489, in wrapper resp = resource(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask/views.py", line 110, in view return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 604, in dispatch_request resp = meth(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/controllers/console/wraps.py", line 147, in decorated return view(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/libs/login.py", line 94, in decorated_view return current_app.ensure_sync(func)(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/controllers/console/wraps.py", line 27, in decorated return view(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/controllers/console/app/wraps.py", line 48, in decorated_view return view_func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 701, in wrapper return marshal(resp, self.fields, self.envelope) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 657, in marshal return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items) ^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 655, in <genexpr> else make(v).output(k, data)) ^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 118, in output value = get_value(key if self.attribute is None else self.attribute, obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 40, in get_value return _get_value_for_keys(key.split('.'), obj, default) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 45, in _get_value_for_keys return _get_value_for_key(keys[0], obj, default) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/flask_restful/fields.py", line 57, in _get_value_for_key return getattr(obj, key, default) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__ return self.impl.get(state, dict_) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables return state._load_expired(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired self.manager.expired_attribute_loader(self, toload, passive) File "/Users/hejl/projects/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Instance <Site at 0x17075f4d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) 2025-03-03 02:12:42,629 INFO [_internal.py:97] 127.0.0.1 - - [03/Mar/2025 02:12:42] "POST /console/api/apps/736f27a7-26d0-434a-9443-d341294874b1/site HTTP/1.1" 500 - ``` ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 18:28:16 -05:00
yindo closed this issue 2026-02-21 18:28:16 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 2, 2025):

The issue you're encountering with the "DetachedInstanceError" in SQLAlchemy is a known problem. This error occurs when an instance of a SQLAlchemy model is not bound to a session, which can happen during a POST request when trying to save settings.

A similar issue was reported, and the suggested solution is to ensure that the model instance is properly managed by the SQLAlchemy session. You can do this by adding the model instance to the session before committing any changes. Here's an example of how you can modify your code to address this issue:

with Session(db.engine) as session:
    site = session.query(Site).filter(Site.app_id == app_model.id).first()

    if not site:
        raise NotFound

    for attr_name in [
        "title",
        "icon_type",
        "icon",
        "icon_background",
        "description",
        "default_language",
        "chat_color_theme",
        "chat_color_theme_inverted",
        "customize_domain",
        "copyright",
        "privacy_policy",
        "custom_disclaimer",
        "customize_token_strategy",
        "prompt_public",
        "show_workflow_steps",
        "use_icon_as_answer_icon",
    ]:
        value = args.get(attr_name)
        if value is not None:
            setattr(site, attr_name, value)

    site.updated_by = current_user.id
    site.updated_at = datetime.now(UTC).replace(tzinfo=None)
    
    # Add the site object to the session
    session.add(site)
    session.commit()

By adding session.add(site), you ensure that the site object is part of the session, which should resolve the error [1].

Additionally, upgrading your Dify installation to version 0.15.0 or later may also help, as this version includes fixes for SQLAlchemy session management issues that were causing similar errors in previous versions [2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Mar 2, 2025): <!-- Answer --> The issue you're encountering with the "DetachedInstanceError" in SQLAlchemy is a known problem. This error occurs when an instance of a SQLAlchemy model is not bound to a session, which can happen during a POST request when trying to save settings. A similar issue was reported, and the suggested solution is to ensure that the model instance is properly managed by the SQLAlchemy session. You can do this by adding the model instance to the session before committing any changes. Here's an example of how you can modify your code to address this issue: ```python with Session(db.engine) as session: site = session.query(Site).filter(Site.app_id == app_model.id).first() if not site: raise NotFound for attr_name in [ "title", "icon_type", "icon", "icon_background", "description", "default_language", "chat_color_theme", "chat_color_theme_inverted", "customize_domain", "copyright", "privacy_policy", "custom_disclaimer", "customize_token_strategy", "prompt_public", "show_workflow_steps", "use_icon_as_answer_icon", ]: value = args.get(attr_name) if value is not None: setattr(site, attr_name, value) site.updated_by = current_user.id site.updated_at = datetime.now(UTC).replace(tzinfo=None) # Add the site object to the session session.add(site) session.commit() ``` By adding `session.add(site)`, you ensure that the `site` object is part of the session, which should resolve the error <sup>[[1]](https://github.com/langgenius/dify/issues/14613)</sup>. Additionally, upgrading your Dify installation to version 0.15.0 or later may also help, as this version includes fixes for SQLAlchemy session management issues that were causing similar errors in previous versions <sup>[[2]](https://github.com/langgenius/dify/issues/12467)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/6a7d511a-78cf-4b91-b05a-f2c0e92d6f56?feedback_type=other)</sup>
Author
Owner

@tcodehuber commented on GitHub (Mar 4, 2025):

I met the same error, which blocked my exploration.

@tcodehuber commented on GitHub (Mar 4, 2025): I met the same error, which blocked my exploration.
Author
Owner

@hjlarry commented on GitHub (Mar 4, 2025):

I met the same error, which blocked my exploration.

please wait for next version released or build the source code yourself

@hjlarry commented on GitHub (Mar 4, 2025): > I met the same error, which blocked my exploration. please wait for next version released or build the source code yourself
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#8871