Skip to content

feat(query-interface): add support for defining composite foreign keys - #12456

Merged
sushantdhiman merged 1 commit into
sequelize:masterfrom
kf6kjg:feat-compositeFKConstraint
Jul 3, 2020
Merged

sushantdhiman merged 1 commit into
sequelize:masterfrom
kf6kjg:feat-compositeFKConstraint

Conversation

@kf6kjg

@kf6kjg kf6kjg commented Jul 2, 2020

Copy link
Copy Markdown
Contributor

Only in the query interface. Extensive support for composite keys is the subject of #311; this is merely one step in the right direction.

Pull Request check-list

  • [yep] Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • [yep] Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • [yep] Have you added new tests to prevent regressions?
  • [yep] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • [can't] Did you update the typescript typings accordingly (if applicable)?
  • [yep] Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Added support for defining composite foreign keys using queryInterface.addConstraint. To accomplish this the references object was extended to support either the existing field:string pair or the new key fields: string[] pair - but not both.

Test was added, documentation updated.

I've attached a patch for the typescript definitions, but the entire typescript typings seem to have been deleted from master in commit 042cd69.

sequelize_composite-foreign-keys_type-change.patch.txt

Only in the query interface.  Extensive support for composite keys is the subject of sequelize#311; this is merely one step in the right direction.
@codecov

codecov Bot commented Jul 2, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #12456 into master will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #12456      +/-   ##
==========================================
+ Coverage   96.44%   96.46%   +0.02%     
==========================================
  Files          95       95              
  Lines        9192     9194       +2     
==========================================
+ Hits         8865     8869       +4     
+ Misses        327      325       -2     
Impacted Files Coverage Δ
src/dialects/abstract/query-interface.js 94.13% <ø> (ø)
src/dialects/abstract/query-generator.js 97.66% <100.00%> (+<0.01%) ⬆️
src/dialects/postgres/connection-manager.js 95.80% <0.00%> (+1.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 188a823...875c7ec. Read the comment docs.

@sushantdhiman
sushantdhiman merged commit a55c154 into sequelize:master Jul 3, 2020
@kf6kjg
kf6kjg deleted the feat-compositeFKConstraint branch July 3, 2020 17:35
@hashinclude72

Copy link
Copy Markdown

can you please explain how to use this ?
Do i set primary = true in multiple columns?

@kf6kjg

kf6kjg commented Jul 8, 2020

Copy link
Copy Markdown
Contributor Author

This only changes the query interface to allow the creation of composite foreign keys. See addContraint options.fields in the docs: https://sequelize.org/master/class/lib/dialects/abstract/query-interface.js~QueryInterface.html#instance-method-addConstraint

Note that the auto generated foreign key name may wind up too long for the database to accept. In that case simply use options.name to specify your own shorter name.

@osvaldokalvaitir

Copy link
Copy Markdown

I see that the problem has already been solved!

How do I use it?
What version of Sequelize do I use?

@frcouceiro

Copy link
Copy Markdown

@kf6kjg Can you open a PR for v6 ?

@ShaunParsons

Copy link
Copy Markdown

I've opened a PR to move this feature over to v6: #12867

papb pushed a commit that referenced this pull request Jan 16, 2021
@papb

papb commented Jan 19, 2021

Copy link
Copy Markdown
Member

I see that the problem has already been solved!

How do I use it?
What version of Sequelize do I use?

Hi @osvaldokalvaitir, sorry to take so long. You can use v6.4.0.

jctaveras pushed a commit to jctaveras/sequelize that referenced this pull request Mar 29, 2021
@kf6kjg

kf6kjg commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

Looks like I missed the typescript file change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants