brianc / node-postgres

PostgreSQL client for node.js.

Home Page:https://node-postgres.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Breaking change in queries with duplicate column names

pgarrett-twc opened this issue · comments

In pg versions prior to 8.11.4, selecting multiple columns with the same name would produce a non-null result if any of the columns were not null. Starting with 8.11.4, query() returns the right-most column of the same name, even if it's null. This is arguably the correct behavior, but it caused a breakage in my application due to a select * outer join query that had multiple columns of the same name from different tables, where some were null and others not.

This appears to have been introduced in da0f5c5.

Reproduction script:

const { Client } = require('pg')

async function main() {
  const client = new Client()
  await client.connect()

  const x = 'Hello, world'
  const res = await client.query(`SELECT $1::text as msg, null as msg`, [x])
  console.dir(res.rows)
  await client.end()
}

main()

8.11.3:

$ npm install pg@8.11.3
...

$ node repro.js
[ { msg: 'Hello, world' } ]

8.11.4:

$ npm install pg@8.11.4
...

$ node repro.js
[ { msg: null } ]

8.11.5:

$ npm install pg@8.11.5
...

$ node repro.js
[ { msg: null } ]

In pg versions prior to 8.11.4

In 8.11.3 only, which was the unintentional breaking change and reverted. #3062

I see. I didn't realize we had been on 8.11.3 for as long as we had. Thanks.

Thanks, we were also relying on this bug without even knowing it.

Adding alias on column names fixes our problems after update from 8.11.3 to 8.11.5