Skip to content

Fixes MSSQL bulkInsertQuery when options and attributes are not passed - #6782

Merged
janmeier merged 1 commit into
sequelize:masterfrom
harshithkashyap:mssql-bulkinsertquery-fix
Nov 2, 2016
Merged

janmeier merged 1 commit into
sequelize:masterfrom
harshithkashyap:mssql-bulkinsertquery-fix

Conversation

@harshithkashyap

@harshithkashyap harshithkashyap commented Nov 1, 2016 •

Copy link
Copy Markdown

Pull Request check-list

  • 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?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Have you added an entry under Future in the changelog?

Description of change

When the queryInterface bulkInsert is used with MSSQL dialect in the sequelize cli seed phase,

Cannot read property 'name' of undefined error is thrown.

queryInterface.bulkInsert internally calls QueryGenerator.bulkInsertQuery and is a separate function in case of MSSQL.

PR Changes:

  • Add default options and attributes to the bulkInsertQuery method.
  • Add a check to see if the attributes object exists before reading autoIncrement property.
  • Unit Tests MSSQL bulkInsertQuery method.

@harshithkashyap

Copy link
Copy Markdown
Author

Pinging @mickhansen @janmeier

@codecov-io

codecov-io commented Nov 1, 2016 •

Copy link
Copy Markdown

Current coverage is 94.50% (diff: 100%)

Merging #6782 into master will increase coverage by <.01%

Powered by Codecov. Last update 5a266dc...ec6d165

@felixfbecker

Copy link
Copy Markdown
Contributor

Was only MSSQL affected by this bug?

@harshithkashyap

Copy link
Copy Markdown
Author

@felixfbecker Yes, Since MSSQL has its own bulkInsertQuery

Comment thread lib/dialects/mssql/query-generator.js Outdated
// special case for empty objects with primary keys
var fields = Object.keys(attrValueHash);
if (fields.length === 1 && attributes[fields[0]].autoIncrement && attrValueHash[fields[0]] === null) {
if (fields.length === 1 && attributes[fields[0]] && attributes[fields[0]].autoIncrement && attrValueHash[fields[0]] === null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This statement has gone a bit complex now. Please extract attributes[fields[0]] as a variable (firstAttr) so we can simplify the checks. May be we can get rid of null check from end as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@sushantdhiman The null check at the end is required. Tests seem to fail.
The null check condition generates the following query if true

INSERT INTO [Workers] DEFAULT VALUES;

MSSQL also allows autogenerated fields to be set explicitly by toggling the IDENTITY_INSERT value.
Eg query from test:

SET IDENTITY_INSERT [Workers] ON; INSERT INTO [Workers] ([id]) VALUES (5),(10); SET IDENTITY_INSERT [Workers] OFF;

@janmeier
janmeier merged commit aa13dc6 into sequelize:master Nov 2, 2016
@janmeier

janmeier commented Nov 2, 2016

Copy link
Copy Markdown
Member

👍

@harshithkashyap
harshithkashyap deleted the mssql-bulkinsertquery-fix branch November 2, 2016 08:35
@harshithkashyap

Copy link
Copy Markdown
Author

@janmeier Thanks for the merge. I need this patch in my project. Should i submit another PR to the v3 branch for it to get released?

@felixfbecker

Copy link
Copy Markdown
Contributor

Yup, you can do a backport

sushantdhiman pushed a commit that referenced this pull request Nov 3, 2016
* V3 backport of MSSQL BulkInsertQuery - #6782

* Update changelog.md
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.

5 participants