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

Commit 71ed0c1a by Mick Hansen

fix(model): dont remove primary key if using default value in upsert, closes #3247

1 parent e9434d0a
...@@ -530,7 +530,7 @@ module.exports = (function() { ...@@ -530,7 +530,7 @@ module.exports = (function() {
options.fields.push(createdAtAttr); options.fields.push(createdAtAttr);
} }
if (primaryKeyAttribute && !primaryKeyAttribute.autoIncrement && options.fields.indexOf(primaryKeyName) < 0) { if (primaryKeyAttribute && primaryKeyAttribute.defaultValue && options.fields.indexOf(primaryKeyName) < 0) {
options.fields.unshift(primaryKeyName); options.fields.unshift(primaryKeyName);
} }
} }
......
...@@ -1224,7 +1224,7 @@ module.exports = (function() { ...@@ -1224,7 +1224,7 @@ module.exports = (function() {
var createdAtAttr = this._timestampAttributes.createdAt var createdAtAttr = this._timestampAttributes.createdAt
, updatedAtAttr = this._timestampAttributes.updatedAt , updatedAtAttr = this._timestampAttributes.updatedAt
, hadPrimary = this.primaryKeyField in values , hadPrimary = this.primaryKeyField in values || this.primaryKeyAttribute in values
, instance = this.build(values); , instance = this.build(values);
return instance.hookValidate(options).bind(this).then(function () { return instance.hookValidate(options).bind(this).then(function () {
...@@ -1240,7 +1240,7 @@ module.exports = (function() { ...@@ -1240,7 +1240,7 @@ module.exports = (function() {
// Build adds a null value for the primary key, if none was given by the user. // Build adds a null value for the primary key, if none was given by the user.
// We need to remove that because of some Postgres technicalities. // We need to remove that because of some Postgres technicalities.
if (!hadPrimary) { if (!hadPrimary && !this.rawAttributes[this.primaryKeyAttribute].defaultValue) {
delete values[this.primaryKeyField]; delete values[this.primaryKeyField];
} }
......
...@@ -92,6 +92,26 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -92,6 +92,26 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('should work with UUIDs wth default values', function () {
var User = this.sequelize.define('User', {
id: {
primaryKey: true,
allowNull: false,
unique: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4
},
name: {
type: Sequelize.STRING,
}
});
return User.sync({ force: true }).then(function () {
return User.upsert({ name: 'John Doe' });
});
});
it('works with upsert on a composite primary key', function() { it('works with upsert on a composite primary key', function() {
var User = this.sequelize.define('user', { var User = this.sequelize.define('user', {
a: { a: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!