Skip to content

feat: support cyclic foreign keys - #14499

Merged
ephys merged 12 commits into
v6from
ephys/cyclic-fks-v6
May 23, 2022
Merged

ephys merged 12 commits into
v6from
ephys/cyclic-fks-v6

Conversation

@ephys

@ephys ephys commented May 11, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • Does yarn test or yarn test-DIALECT pass with this change (including linting)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

Closes #1717

Note that this PR targets v6, I'll bring it into v7 once merged.

This PR fixes the support for circular foreign key references. (e.g. model A that has a foreign key referencing model B, while B also has a foreign key referencing model A).

This was previously causing Sequelize#sync, Sequelize#drop and Sequelize#truncate to throw because:

  • Sequelize#sync tried to create foreign keys before the target table was created. The updated version still does by default, but splits generation in two steps (table creation, then foreign key constraint creation) if there is a cyclic reference.
  • Sequelize#drop tried to drop tables that were referenced by other tables. The updated version still does by default but if there is a cyclic reference, it will first remove all foreign keys before dropping all tables.
  • Sequelize#truncate tried to delete rows that were referenced by other tables. This is left unchanged, as the solution is to use the cascade option. (for dialects that support CASCADE)

TODO

  • Add typings for the new methods
  • Ensure integration tests are OK

@ephys ephys self-assigned this May 11, 2022
@ephys
ephys marked this pull request as ready for review May 12, 2022 08:24
@4-1-8

4-1-8 commented May 16, 2022

Copy link
Copy Markdown

Nice, we have exactly this problem!

WikiRik
WikiRik previously approved these changes May 16, 2022

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two comments, but looks good!

Comment thread dev/mssql/2019/start.sh Resolved
Comment thread src/dialects/mysql/query-generator.js Resolved
@WikiRik

WikiRik commented May 17, 2022

Copy link
Copy Markdown
Member

One test seems to remain broken;

  1) [MYSQL] Include
       findAll
         should support including date fields, with the correct timeszone:
     Table 'sequelize_test.users' doesn't exist
  Error
      at Query.run (lib/dialects/mysql/query.js:52:25)
      at /home/runner/work/sequelize/sequelize/lib/sequelize.js:311:28
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async MySQLQueryInterface.showIndex (lib/dialects/abstract/query-interface.js:254:12)
      at async Function.sync (lib/model.js:988:29)
      at async Sequelize.sync (lib/sequelize.js:373:9)
      at async Context.<anonymous> (test/integration/include/findAll.test.js:1563:7)

@ephys can you check this out?

@ephys

ephys commented May 17, 2022

Copy link
Copy Markdown
Member Author

Yep :)

@ephys
ephys requested a review from WikiRik May 20, 2022 10:19
@WikiRik

WikiRik commented May 23, 2022

Copy link
Copy Markdown
Member

@ephys I'll leave the merging up to you as it will create a new release

@ephys
ephys merged commit b37df96 into v6 May 23, 2022
@ephys
ephys deleted the ephys/cyclic-fks-v6 branch May 23, 2022 13:17
@ephys

ephys commented May 23, 2022

Copy link
Copy Markdown
Member Author

Here we go then :)

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.20.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@samiurprapon

Copy link
Copy Markdown

With this support do I have to add constraints: false ?

Reference : Documentation

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants