Skip to content

v3: sourceKey feature for hasMany doesn't work if where clause specified in include (#7141) - #7147

Merged
janmeier merged 15 commits into
sequelize:v3from
LoneWolfPR:7141
Jan 31, 2017
Merged

janmeier merged 15 commits into
sequelize:v3from
LoneWolfPR:7141

Conversation

@LoneWolfPR

@LoneWolfPR LoneWolfPR commented Jan 25, 2017 •

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 your issue contain a link to existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Have you added an entry under Future in the changelog?

NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.

Description of change

This fixes the issue mentioned in #7141. Previously sourceKey was only respected in a find from hasMany association if the include did not contain a where clause. This fixes that issue so that even with a where clause specified the sourceKey supplied by the user will be respected.

@mention-bot

Copy link
Copy Markdown

@LoneWolfPR, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mbroadst, @janmeier and @mickhansen to be potential reviewers.

@codecov-io

codecov-io commented Jan 25, 2017 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (v3@2110276).


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 2110276...5b4e078. Read the comment docs.

@LoneWolfPR

Copy link
Copy Markdown
Contributor Author

Any chance this can get merged in sometime soon (assuming it meets all merge requirements)? We have a project that needs this pretty badly with multiple developers needing it.

}

query += ';';

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.

Please remove the whitespace changes (it makes the changes easier to cherry-pick onto master)

describe('sourceKey with where clause in include', function() {
beforeEach(function() {
var User = this.sequelize.define('User',
{ username: Sequelize.STRING, email: Sequelize.STRING },

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.

Could you change it to something that uses field (email: { type: Sequelize.STRING, field: 'mail' } or similar - So we can make sure this works (and continues to work) even when the field is called something other in the DB than in the model

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.

Username attribute and indexes are not needed for this test

@janmeier janmeier self-assigned this Jan 30, 2017
@LoneWolfPR

Copy link
Copy Markdown
Contributor Author

@janmeier I've addressed all your requests. I believe it should be good to go now. Thanks for reviewing. Let me know if there are anymore issues.

@janmeier
janmeier merged commit 3168d54 into sequelize:v3 Jan 31, 2017
janmeier pushed a commit that referenced this pull request Jan 31, 2017
v3: sourceKey feature for hasMany doesn't work if where clause specified in include (#7141) (#7147)

* Initial Commit

* Stops test from removing test/integration/assets/es6project.js

* Fixes impromper syntax when specifying the use of association.sourceIdentifier or left.primaryKeyAttribute for attrLeft. Also includes integration test.

* Updates changelog.

* Adds link to issue in changelog entry.

* Added 'Future' header to changelog entry

* Removed changelog update as requested.

* Updates changelog. Fixes query-generator to properly build queries from hasMany even with an include while respecting a specified sourceKey. Adds an integration test.

* Added back removed whitespace per request.

* Updates test in include altered field name in source key. Fixes were then made to address the test breaking. All tests pass now.

* Adds back removed whitespace.
@janmeier

Copy link
Copy Markdown
Member

Cherry-picked in 7d3172d

@janmeier

Copy link
Copy Markdown
Member

Thanks for the adressing my feedback quickly @LoneWolfPR 🎉

@LoneWolfPR

Copy link
Copy Markdown
Contributor Author

@janmeier no problem. Glad I could help. What's the timeline for this going out to the npm?

@LoneWolfPR
LoneWolfPR deleted the 7141 branch January 31, 2017 15:09
@janmeier

Copy link
Copy Markdown
Member

+ [email protected]

@danconnell

Copy link
Copy Markdown
Contributor

Thanks @LoneWolfPR. Just found this bug myself and was very happy to see it fixed already 😄

mkaufmaner pushed a commit to mkaufmaner/sequelize that referenced this pull request Apr 25, 2017
v3: sourceKey feature for hasMany doesn't work if where clause specified in include (sequelize#7141) (sequelize#7147)

* Initial Commit

* Stops test from removing test/integration/assets/es6project.js

* Fixes impromper syntax when specifying the use of association.sourceIdentifier or left.primaryKeyAttribute for attrLeft. Also includes integration test.

* Updates changelog.

* Adds link to issue in changelog entry.

* Added 'Future' header to changelog entry

* Removed changelog update as requested.

* Updates changelog. Fixes query-generator to properly build queries from hasMany even with an include while respecting a specified sourceKey. Adds an integration test.

* Added back removed whitespace per request.

* Updates test in include altered field name in source key. Fixes were then made to address the test breaking. All tests pass now.

* Adds back removed whitespace.
@derimagia

derimagia commented May 17, 2017 •

Copy link
Copy Markdown

I've been having an issue here where the field name is not the same as the model property name. So, if the 'field' is set and it's different the column name this isn't working unless you use the field instead of the column name, unlike any of the other properties.

if (this.target.rawAttributes[this.sourceKey]) {
this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey;
} else {
this.sourceKeyField = this.sourceKey;
}
if (this.source.fieldRawAttributesMap[this.sourceKey]) {
this.sourceKeyAttribute = this.source.fieldRawAttributesMap[this.sourceKey].fieldName;
} else {
this.sourceKeyAttribute = this.source.primaryKeyAttribute;
}

Line 88 is using this.target and line 89 is using this.source, shouldn't they both be checking the source?

Once this.sourceKeyFieldis fixed then this.sourceKey just needs to be replaced by it on lines 94-95. I'd be happy yo put a pull request if need be but wanted to check with someone who knows this more to correct me if I'm missing something.

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.

6 participants