Skip to content

Commit

Permalink
removeAttribute('id') results in undefined: null data value (sequeli…
Browse files Browse the repository at this point in the history
…ze#7318) (sequelize#7382)

* issue # 7318

fixes issue # 7318 - sequelize#7318

* test and change log

* formatting

* changed message

* Update changelog.md

* Update removeAttribute.test.js
  • Loading branch information
shadowwarior5 authored and mkaufmaner committed Apr 25, 2017
1 parent bd29fa4 commit 0f5b748
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- [FIXED] Connection error when fetching OIDs for unspported types in Postgres 8.2 or below [POSTGRES] [#5254](https://github.com/sequelize/sequelize/issues/5254)
- [FIXED] Expose OptimisticLockError on Sequelize object [#7291](https://github.com/sequelize/sequelize/pull/7291)
- [FIXED] Deleted paranoid records can be queried in the same second. [#7204](https://github.com/sequelize/sequelize/issues/7204)/[#7332](https://github.com/sequelize/sequelize/pull/7332)
- [FIXED] `removeAttribute('id')` results in `undefined: null` data value [#7318](https://github.com/sequelize/sequelize/issues/7318)
- [FIXED] `bulkCreate` now runs in O(N) time instead of O(N^2) time. [#4247](https://github.com/sequelize/sequelize/issues/4247)

## BC breaks:
Expand Down
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2908,7 +2908,7 @@ class Model {
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
// do after default values since it might have UUID as a default value
if (!defaults.hasOwnProperty(this.constructor.primaryKeyAttribute)) {
if (this.constructor.primaryKeyAttribute && !defaults.hasOwnProperty(this.constructor.primaryKeyAttribute)) {
defaults[this.constructor.primaryKeyAttribute] = null;
}

Expand Down
11 changes: 11 additions & 0 deletions test/unit/model/removeAttribute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,16 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(Model.primaryKeyAttribute).to.be.undefined;
expect(_.size(Model.primaryKeys)).to.equal(0);
});

it('should not add undefined attribute after removing primary key', function () {
var Model = current.define('m', {
name: DataTypes.STRING
});

Model.removeAttribute('id');

const instance = Model.build();
expect(instance.dataValues).not.to.include.keys('undefined');
});
});
});

0 comments on commit 0f5b748

Please sign in to comment.