Skip to content

Commit 106ca8a

Browse files
authored
Fix get value of last column with same name in result rows (#3063)
* Add failing test for result rows with the same column names * Fix handling of duplicate column names in results to ensure last value is populated Fixes handling of result rows that have the same column name duplicated in the results to ensure that the last value is the one returned to the user. This was the old behavior but unintentionally broken when the pre-built object optimization was added.
1 parent a84ebb3 commit 106ca8a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/pg/lib/result.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Result {
6767
var field = this.fields[i].name
6868
if (rawValue !== null) {
6969
row[field] = this._parsers[i](rawValue)
70+
} else {
71+
row[field] = null
7072
}
7173
}
7274
return row
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
const helper = require('../test-helper')
3+
var assert = require('assert')
4+
const suite = new helper.Suite()
5+
6+
// https://github.com/brianc/node-postgres/issues/3062
7+
suite.testAsync('result fields with the same name should pick the last value', async () => {
8+
const client = new helper.pg.Client()
9+
await client.connect()
10+
11+
const { rows: [shouldBeNullRow] } = await client.query('SELECT NULL AS test, 10 AS test, NULL AS test')
12+
assert.equal(shouldBeNullRow.test, null)
13+
14+
const { rows: [shouldBeTwelveRow] } = await client.query('SELECT NULL AS test, 10 AS test, 12 AS test')
15+
assert.equal(shouldBeTwelveRow.test, 12)
16+
17+
const { rows: [shouldBeAbcRow] } = await client.query(`SELECT NULL AS test, 10 AS test, 12 AS test, 'ABC' AS test`)
18+
assert.equal(shouldBeAbcRow.test, 'ABC')
19+
20+
await client.end()
21+
})

0 commit comments

Comments
 (0)