Skip to content

Commit

Permalink
fix(mssql): insert/upsert operations do not return all fields (#12433)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharHD committed Jun 27, 2020
1 parent 962ed3c commit aeb318a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/dialects/mssql/query.js
Expand Up @@ -380,6 +380,19 @@ class Query extends AbstractQuery {
id = id || autoIncrementAttributeAlias && results && results[0][autoIncrementAttributeAlias];

this.instance[autoIncrementAttribute] = id;

if (this.instance.dataValues) {
for (const key in results[0]) {
if (Object.prototype.hasOwnProperty.call(results[0], key)) {
const record = results[0][key];

const attr = _.find(this.model.rawAttributes, attribute => attribute.fieldName === key || attribute.field === key);

this.instance.dataValues[attr && attr.fieldName || key] = record;
}
}
}

}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/integration/model/create.test.js
Expand Up @@ -1420,4 +1420,21 @@ describe(Support.getTestDialectTeaser('Model'), () => {

expect(spy.called).to.be.ok;
});

if (current.dialect.supports.returnValues) {
it('should return default value set by the database (create)', async function() {

const User = this.sequelize.define('User', {
name: DataTypes.STRING,
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});

await User.sync({ force: true });

const user = await User.create({ name: 'FooBar' });

expect(user.name).to.be.equal('FooBar');
expect(user.code).to.be.equal(2020);
});
}
});
20 changes: 20 additions & 0 deletions test/integration/model/upsert.test.js
Expand Up @@ -577,6 +577,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
});

it('should return default value set by the database (upsert)', async function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING, primaryKey: true },
code: { type: Sequelize.INTEGER, defaultValue: Sequelize.literal(2020) }
});

await User.sync({ force: true });

const [user, created] = await User.upsert({ name: 'Test default value' }, { returning: true });

expect(user.name).to.be.equal('Test default value');
expect(user.code).to.be.equal(2020);

if (dialect === 'sqlite' || dialect === 'postgres') {
expect(created).to.be.null;
} else {
expect(created).to.be.true;
}
});
}
});
}
Expand Down

0 comments on commit aeb318a

Please sign in to comment.