不要怂,就是干,撸起袖子干!

Commit 047acc4b by Mick Hansen

fix(model): create() using fields not specifying and non-incremental primary key…

… now workers ok, closes #3458
1 parent c4cfe9f1
# Next # Next
- [BUG] Fix for newlines in hstore [#3383](https://github.com/sequelize/sequelize/issues/3383) - [BUG] Fix for newlines in hstore [#3383](https://github.com/sequelize/sequelize/issues/3383)
- [BUG] Fix unique key handling in Model.update [#3474](https://github.com/sequelize/sequelize/issues/3474) - [BUG] Fix unique key handling in Model.update [#3474](https://github.com/sequelize/sequelize/issues/3474)
- [BUG] Fix issue with Model.create() using fields not specifying and non-incremental primary key [#3458](https://github.com/sequelize/sequelize/issues/3458)
- [FEATURE] `field` support for Model.update [#3498](https://github.com/sequelize/sequelize/pull/3498) - [FEATURE] `field` support for Model.update [#3498](https://github.com/sequelize/sequelize/pull/3498)
- [INTERNALS] Updated dependencies. Most notably we are moving up one major version on lodash. If you are using `sequelize.Utils._`, notice that the semantics for many matching functions have changed to include a check for `hasOwnProperty` - [INTERNALS] Updated dependencies. Most notably we are moving up one major version on lodash. If you are using `sequelize.Utils._`, notice that the semantics for many matching functions have changed to include a check for `hasOwnProperty`
+ dottie@0.3.1 + dottie@0.3.1
......
...@@ -506,6 +506,8 @@ module.exports = (function() { ...@@ -506,6 +506,8 @@ module.exports = (function() {
} }
var self = this var self = this
, primaryKeyName = this.Model.primaryKeyAttribute
, primaryKeyAttribute = primaryKeyName && this.Model.rawAttributes[primaryKeyName]
, updatedAtAttr = this.Model._timestampAttributes.updatedAt , updatedAtAttr = this.Model._timestampAttributes.updatedAt
, createdAtAttr = this.Model._timestampAttributes.createdAt , createdAtAttr = this.Model._timestampAttributes.createdAt
, hook = self.isNewRecord ? 'Create' : 'Update' , hook = self.isNewRecord ? 'Create' : 'Update'
...@@ -523,8 +525,14 @@ module.exports = (function() { ...@@ -523,8 +525,14 @@ module.exports = (function() {
updatedAtAttr = false; updatedAtAttr = false;
} }
if (this.isNewRecord === true && createdAtAttr && options.fields.indexOf(createdAtAttr) === -1) { if (this.isNewRecord === true) {
options.fields.push(createdAtAttr); if (createdAtAttr && options.fields.indexOf(createdAtAttr) === -1) {
options.fields.push(createdAtAttr);
}
if (primaryKeyAttribute && !primaryKeyAttribute.autoIncrement && options.fields.indexOf(primaryKeyName) < 0) {
options.fields.unshift(primaryKeyName);
}
} }
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
......
...@@ -440,6 +440,32 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -440,6 +440,32 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('works with non-integer primary keys with fields excluding the primary key', function () {
var User = this.sequelize.define('User', {
'id': {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false
},
'email': {
type: DataTypes.STRING
}
});
return this.sequelize.sync({force: true}).then(function() {
return User.create({
email: Math.random().toString()
}, {
fields: ['email'],
logging: console.log
}).then(function(user) {
expect(user).to.be.ok;
expect(user.get('id')).to.be.ok;
});
});
});
it('should return an error for a unique constraint error', function() { it('should return an error for a unique constraint error', function() {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
'email': { 'email': {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!