Skip to content

Commit

Permalink
fix(query): do not omit field when same field/name in fieldMap (#11492)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwpmad authored and sushantdhiman committed Oct 1, 2019
1 parent c7138f5 commit 7a90df5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dialects/abstract/query.js
Expand Up @@ -248,7 +248,7 @@ class AbstractQuery {
if (this.options.fieldMap) {
const fieldMap = this.options.fieldMap;
results = results.map(result => _.reduce(fieldMap, (result, name, field) => {
if (result[field] !== undefined) {
if (result[field] !== undefined && name !== field) {
result[name] = result[field];
delete result[field];
}
Expand Down
12 changes: 12 additions & 0 deletions test/integration/sequelize.test.js
Expand Up @@ -468,6 +468,18 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
});
});

it('keeps field names that are mapped to the same name', function() {
return this.sequelize.query(this.insertQuery).then(() => {
return this.sequelize.query(`SELECT * FROM ${qq(this.User.tableName)};`, {
type: 'SELECT',
fieldMap: { username: 'username', email_address: 'email' }
});
}).then(users => {
expect(users[0].username).to.be.equal('john');
expect(users[0].email).to.be.equal('john@gmail.com');
});
});

it('reject if `values` and `options.replacements` are both passed', function() {
return this.sequelize.query({ query: 'select ? as foo, ? as bar', values: [1, 2] }, { raw: true, replacements: [1, 2] })
.should.be.rejectedWith(Error, 'Both `sql.values` and `options.replacements` cannot be set at the same time');
Expand Down

0 comments on commit 7a90df5

Please sign in to comment.