-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(core): Add composite foreign key support for associations #17765
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
Open
gtamasi
wants to merge
63
commits into
sequelize:main
Choose a base branch
from
papandreou:gtamasi/sequelize-composite-foreign-key-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(core): Add composite foreign key support for associations #17765
gtamasi
wants to merge
63
commits into
sequelize:main
from
papandreou:gtamasi/sequelize-composite-foreign-key-support
+1,873
−279
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the "known good" state of the findbypk-composite-support branch * commit '0717a42ad': change misused property check if model has pk first prettier changes code review changes prettier changes add composite key findByPk support via object
…patch' into sequelize-core-papandreou9 * feature/composite-primary-key-support-and-associations-patch: Fix issue with includes and hasmany use ? to check if it is safe to push to array in hasmany add missing tests cleanup models file add constraintName property update types consolidate changes from other branches
(cherry picked from commit df19318)
* main: fix(core): Adjust model validator types yet again (sequelize#17689)
… gtamasi/sequelize-composite-foreign-key-support
…in association options
…nd foreignKey.keys
…keys and composite foreign key options
…ny and BelongsTo associations
gtamasi
commented
Apr 9, 2025
| /** | ||
| * The pairs of the foreign key attributes used for composite foreign keys. | ||
| */ | ||
| keys?: ForeignKey[] | Array<CompositeForeignKeysOptions<ForeignKey, SourceKey>>; |
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We consciously went down this road to make this change backwards-compatible. We discussed the API design choice proposed in #311, but realized that by having foreignKeys as an array would make it impossible to define constraints and cascading for the composite key itself.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Description of Changes
Currently, Sequelize primarily supports single-column foreign keys. However, many database schemas utilize composite foreign keys to enforce data integrity and model complex relationships more accurately. This feature addresses this limitation and provides greater flexibility in defining database mappings.
Issue: #311
Changes Introduced:
foreignKeyoption inbelongsTo,hasOne, andhasManyassociations contains an extra field calledkeyswhich holds the keys used for composite keys. This allows specifying multiple columns that constitute the foreign key in the source model.sourceKeyandtargetKeyattribute. The logic is the same as if you use thetargetKeyattribute in case of single column key.With this design, the aim was the backwards compatibility so this PR contains no breaking changes.
Example Usage:
Notes
In SQLite3, you only get autoincrement behavior when only one integer column is the primary key. Therefore, you cannot define a composite primary key when one of the column defined as
AUTOINCREMENT.You can get a similar result by defining a column as the only primary key, but then adding an additional unique constraint on columns.
List of Breaking Changes