Skip to content

fix(scope): include scope on eagerly created associations - #9127

Merged
sushantdhiman merged 1 commit into
sequelize:masterfrom
gjacobrobertson:create-included-scope
Mar 10, 2018
Merged

sushantdhiman merged 1 commit into
sequelize:masterfrom
gjacobrobertson:create-included-scope

Conversation

@gjacobrobertson

@gjacobrobertson gjacobrobertson commented Mar 1, 2018 •

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

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

Description of change

Models being created with included associations do not include the association scope on the created models. Example:

const Post = sequelize.define('post', {});
const Comment = sequelize.define('comment', {
  title: Sequelize.STRING,
  commentable: Sequelize.STRING,
  commentable_id: Sequelize.INTEGER
});
Post.Comments = Post.hasMany(Comment, {
  foreignKey: 'commentable_id',
  scope: {
    commentable: 'post'
  },
  constraints: false
});

const post = Post.create({
  comments:[{
    title: 'This is a comment'
  }]
}, {
  include: [Post.Comments]
});

// post.comments[0].commentable == null

i.e. association scope is only injected when creating via Post.Comments.create (and methods that call it), and not with Post.create({comments: [...], include:[Post.Comments]})

This PR causes scope to be injected on included associations at parent model build time, such that
in the previous example, post.comments[0].commentable would be set to 'post'

@codecov

codecov Bot commented Mar 1, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9127 into master will increase coverage by <.01%.
The diff coverage is 100%.

@gjacobrobertson
gjacobrobertson force-pushed the create-included-scope branch from 53abb98 to 67053ed Compare March 1, 2018 20:43
@gjacobrobertson gjacobrobertson changed the title Created included assocations with scope fix(scope): include scope on eagerly created associations Mar 1, 2018
@sushantdhiman
sushantdhiman requested a review from a team March 10, 2018 08:52
@sushantdhiman
sushantdhiman merged commit 969978d into sequelize:master Mar 10, 2018
@sushantdhiman

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 4.35.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

3 participants