-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Joins with overlapping column names, nested results #733
Comments
I definitely feel your pain on this issue, I've run into it myself. Problem is there's no really good way to fix it in postgres & I think doing that much fanciness with the return values is outside the scope of node-postgres itself. Also: postgres unfortunately doesn't return table names in the query response, only the table OID so an additional query would be needed to look up the table names. If you have overlapping column names you can use |
Here's a test we have with an example of how to enable results to come back as an array: If the results don't come back as an array |
Here's basically how I do it: let obj1 = {...};
let obj2 = {...};
for (let key in row) {
if (key in obj1) obj1[key] = row[key];
if (key in obj2) obj2[key] = row[key];
} Thankfully my overlapping columns share the same value, so I don't suffer any data loss, but it's less elegant than receiving the objects made for me as I have to redefine the table content on the js-side. |
@brianc let text = `SELECT to_json(a.*) AS table1, to_json(b.*) AS table2
FROM some_table a
LEFT JOIN another_table b ON a.id = b.id`;
client.query(text, function(res) {
// ...
}); With this all rows will be structured as such: { table1: { ... },
table2: { ... } } |
Nice, yeah. I've done some stuff with On Sat, Feb 21, 2015 at 3:40 PM, Mayhem notifications@github.com wrote:
|
@brianc thanks mate! Your solution is the only one with 100% benefits! For fellow googlers, this gives in knex: return knex
.select(knex.raw('row_to_json("users__netlify".*) AS "users__netlify", row_to_json("users".*) AS "users"')).from('users')
.fullOuterJoin('users__netlify', {'users.id': 'users__netlify.user_id'})
.where('users__netlify.own_id', netlify_id)
.then(result => {
console.log('outer join result', result)
}) |
@brianc
https://github.com/felixge/node-mysql/#joins-with-overlapping-column-names
It is possible to have a similar functionality in node-postgres?
For example:
The text was updated successfully, but these errors were encountered: