Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: join primary key to unique constraint #7128

Closed
wants to merge 2 commits into from

Conversation

lpdink
Copy link
Contributor

@lpdink lpdink commented Aug 9, 2024

Checklist:

Important

Please review the checklist below before submitting your pull request.

  • Please open an issue before creating a PR or link to an existing issue
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Description

This submission modifies the table definitions by adding the primary key id to all UniqueConstraint constraints, ensuring that table creation succeeds in a distributed database. This is because in a distributed database, the distribution key must be a subset of the unique keys in order to perform deduplication. If there are two unique keys, then the distribution key needs to be selected from the intersection of these two unique keys. This PR addresses the issue discussed in #6720. However, this requires that all future unique declarations must include the primary key to ensure persistent effectiveness.

I do understand that this may introduce some burden, but I kindly ask the maintainers to consider this suggestion as it will help Dify support large-scale distributed deployments.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

Testing Instructions

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Verified that the updated ORM definitions no longer produce the unique constraint error in a distributed database environment.
  • Checked that the primary key and unique constraints work as expected in single-node database

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 💪 enhancement New feature or request labels Aug 9, 2024
@lpdink
Copy link
Contributor Author

lpdink commented Aug 9, 2024

@bowenliang123 Could you please kindly review this PR?

@bowenliang123
Copy link
Contributor

cc @takatost

Copy link
Collaborator

@takatost takatost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution.
However, I’d like to point out that the proposed modification—adding the id field to existing unique constraints—could undermine the intended uniqueness of the original combinations, such as account_id + provider or any similar pair. Since id is always unique, the uniqueness constraint on these combinations would no longer prevent duplicate entries for the same values, as the uniqueness would be enforced only when the id differs.

@bowenliang123
Copy link
Contributor

Thank you for your contribution. However, I’d like to point out that the proposed modification—adding the id field to existing unique constraints—could undermine the intended uniqueness of the original combinations, such as account_id + provider or any similar pair. Since id is always unique, the uniqueness constraint on these combinations would no longer prevent duplicate entries for the same values, as the uniqueness would be enforced only when the id differs.

+1.

@crazywoola crazywoola closed this Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants