-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
When two result columns have the same name, one is dropped #280
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
Comments
This is pretty easy to do with the JavaScript API, but not very easy with the native bindings. The JavaScript API uses a pg.connect(function(err, client, done) {
client.connection.on('rowDescription', function(msg) {
//figure out some way to make sense of two columns with the same name
});
client.connection.on('dataRow', function(msg) {
//map the data rows to result structures any way you see fit
});
}); The code above is similar but slightly simplified to how node-postgres itself handles the messages. Here's how node-postgres parses the row information: https://github.com/brianc/node-postgres/blob/master/lib/query.js#L58 |
Awesome, thanks for the quick feedback! I will give the JavaScript API a try, but would eventually be interested in adding this capability to the native bindings. Is that something you'd want to incorporate? |
Also, how do you link the |
It can definitely be added to both. Having thought about this a bit more here's what we currently have when we run a query: var result = {
rows:[{ colName: 'val1', colName2: 'val2' }]
} We could modify it to be this: var result = {
rows: [...]
values: ['val1', 'val2']
} The only thing I don't like about this exactly is the performance implications of creating an array in every result set. Maybe it could be opt-ip: pg.connect(function(err, client, done) {
var query = client.query('SELECT NOW()', function(err, res) {
console.log(res.values); //['2013-02-23 11:11:11T'];
});
query.includeValues = true;
}); Thoughts? |
It seems like you want to either get the object or the row, but not both. Don't want to create two representations of a large result set if you'll only use one. If you could get just values, you save the memory footprint of storing a copy of the column names for every row. Python's psycopg2 gives you arrays by default but lets you pass in a http://www.initd.org/psycopg/docs/extras.html That seems like a pretty reasonable way to do it. |
Good point about not needing both the object & array type for the same query. Something like a cursor factory would work. Even if it was just as simple as the |
To answer your earlier question: |
Okay, that's what I figured. Thanks for the help. On Sat, Feb 23, 2013 at 3:17 PM, Brian C [email protected] wrote:
|
Why not use an alias?
|
Yea definitely. I think @davidcrawford's original problem was he wasn't in control of the query so he couldn't add an alias to them. The good news is this has been closed by a pull request! |
Thanks much for this solution - but just wondering, is it really a limitation of the server that they don't return table information along with the result? e.g. table1.duplicateColumnName vs table2.duplicateColumnName It just seems odd that although you don't lose data, you still don't know which table the field is from. |
This most commonly happens when issuing queries with functions. If you don't explicitly alias the result, the column is named after the function. For instance:
As far as I can tell, node-postgres only returns rows as objects (not arrays, e.g.). So the second 'avg' overwrites the first one in the object.
That's a great default, but since I'm using node-postgres to build a web client for sql analytic queries, I have to expect my users to write queries like this.
I'd like to propose an addition to the API of an option to emit rows as arrays (or maybe even just a separate event like 'row-array'). I'd love your feedback on whether this is something you'd be interested in pulling into the library, and how you'd recommend doing it.
One consideration is that if you send back an array, you don't get column names. I would propose a 'columns' event that fires before any row events and sends the column information for rows to come. This would be one way of addressing issue #209.
I'm happy to help out on this, though I have no familiarity with the V8 engine so it would be great to get pointers to good docs.
The text was updated successfully, but these errors were encountered: