The database has no Foreign Keys #12388

Closed
opened 2026-02-21 19:07:11 -05:00 by yindo · 3 comments
Owner

Originally created by @DavideDelbianco on GitHub (Mar 31, 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

1.1.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Explore the database schema.

Even though many tables have columns referencing other tables, the Foreign Keys are not set, making the Relational Database prone to errors if something is manually deleted from a table (should not be done) or if something in the code does async operations to remove rows from tables like in my other issue #17193

✔️ Expected Behavior

Relational Databases should have foreign keys constraints set, to ensure relational consistency

Actual Behavior

There are no Foreign Key constraints

Image

Originally created by @DavideDelbianco on GitHub (Mar 31, 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 1.1.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Explore the database schema. Even though many tables have columns referencing other tables, the Foreign Keys are not set, making the Relational Database prone to errors if something is manually deleted from a table (should not be done) or if something in the code does async operations to remove rows from tables like in my other issue #17193 ### ✔️ Expected Behavior Relational Databases should have foreign keys constraints set, to ensure relational consistency ### ❌ Actual Behavior There are no Foreign Key constraints ![Image](https://github.com/user-attachments/assets/2bba0370-8031-4047-aa8f-00b3a1e85964)
yindo added the 🐞 bug label 2026-02-21 19:07:11 -05:00
yindo closed this issue 2026-02-21 19:07:11 -05:00
Author
Owner

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

The absence of foreign key constraints in the Dify database schema can lead to potential issues with data integrity, especially if manual deletions or asynchronous operations occur. While the specific reasons for not implementing foreign keys in Dify are not documented in the available resources, there are some general considerations that might explain this design choice:

  1. Performance: Foreign key constraints can impact database performance, especially in high-load environments. By not using them, the system might achieve better performance at the cost of manual data integrity management.

  2. Flexibility: Without foreign keys, developers have more flexibility in managing data, which can be beneficial in certain scenarios where data relationships are complex or evolving.

  3. Application-Level Integrity: It's possible that Dify relies on application-level logic to maintain data integrity, using code to enforce relationships and constraints instead of relying on the database.

For a more detailed understanding, you might want to explore the migration scripts and models in the Dify codebase, as they can provide insights into the current schema and its relationships [1][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 31, 2025): <!-- Answer --> The absence of foreign key constraints in the Dify database schema can lead to potential issues with data integrity, especially if manual deletions or asynchronous operations occur. While the specific reasons for not implementing foreign keys in Dify are not documented in the available resources, there are some general considerations that might explain this design choice: 1. **Performance**: Foreign key constraints can impact database performance, especially in high-load environments. By not using them, the system might achieve better performance at the cost of manual data integrity management. 2. **Flexibility**: Without foreign keys, developers have more flexibility in managing data, which can be beneficial in certain scenarios where data relationships are complex or evolving. 3. **Application-Level Integrity**: It's possible that Dify relies on application-level logic to maintain data integrity, using code to enforce relationships and constraints instead of relying on the database. For a more detailed understanding, you might want to explore the migration scripts and models in the Dify codebase, as they can provide insights into the current schema and its relationships <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/community/contribution.md)</sup><sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/community/contribution.md)</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/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/784aecae-be2b-4308-baea-e9f547543c10?feedback_type=other)</sup>
Author
Owner

@QuantumGhost commented on GitHub (Mar 31, 2025):

Thank you for reporting this issue.

Dify does use conceptual foreign keys within its schema design. However, these foreign keys are not enforced at the database level. This decision was made to simplify data migrations and schema evolution.

Additionally, since Dify is also provided as a cloud service, omitting foreign key constraints at the database level helps facilitate horizontal scalability and operational flexibility.

If enforcing foreign keys is essential for your use case, could you kindly provide more details about your specific requirements? Understanding your use case will help us assess this feature more thoroughly.

@QuantumGhost commented on GitHub (Mar 31, 2025): Thank you for reporting this issue. Dify does use conceptual foreign keys within its schema design. However, these foreign keys are not enforced at the database level. This decision was made to simplify data migrations and schema evolution. Additionally, since Dify is also provided as a cloud service, omitting foreign key constraints at the database level helps facilitate horizontal scalability and operational flexibility. If enforcing foreign keys is essential for your use case, could you kindly provide more details about your specific requirements? Understanding your use case will help us assess this feature more thoroughly.
Author
Owner

@DavideDelbianco commented on GitHub (Mar 31, 2025):

Hi @QuantumGhost The use case was the application being in a broken status after deleting a workflow version. (affecting multiple workflows linked to the same workflow-as-tool)

It would have not broken if the DB linked the field "version" of the tables "workflow" and "tool_workflow_providers" preventing the deletion of the row from "workflows", but since the issue is caused by an oversight in the backend/frontend logic, I've decided to close this issue and leave #17193 open

They will probably resolve by adding backend logic and a nice little tag reporting "Tool Version" in the workflow history

@DavideDelbianco commented on GitHub (Mar 31, 2025): Hi @QuantumGhost The use case was the application being in a broken status after deleting a workflow version. (affecting multiple workflows linked to the same workflow-as-tool) It would have not broken if the DB linked the field "version" of the tables "workflow" and "tool_workflow_providers" preventing the deletion of the row from "workflows", but since the issue is caused by an oversight in the backend/frontend logic, I've decided to close this issue and leave #17193 open They will probably resolve by adding backend logic and a nice little tag reporting "Tool Version" in the workflow history
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#12388