Reputation: 35813
I'm making a query with Knex (PostgreSQL):
knex('foos')
.select(columnsThatAreAMixOfFooAndBarColumns)
.leftJoin('bars', 'foos.bar_id', 'bars.id')
.where({ 'foos.id': id })
When I get back results, I don't get back [{foo_id: 1, bar_id: 2, ...
. Instead I get back [{ id: 1, ...
.
What's so bizzare is that Knex is building the right query: if I log it I'll see SELECT "foos"."id", "bars"."id", ...
. But for some strange reason I can't figure out, Knex decides to merge all the resulting columns together, so I lose bars.id
entirely and foos.id
gets renamed to id
.
Can anyone explain what's going on, and how I can fix it? I've tried adding returning
, using columns
instead of select
, and otherwise changing how I make the query, but it doesn't help, and again the query Knex builds isn't the problem: it's the results.
EDIT: I re-ran the same exact query using knex.raw(query)
instead ... and got the exact same (id
, not foos_id
) result. So it's definitely not how the query is made that's the problem: I'm somehow doing something to make Knex build the results in a way that ignores the qualified column names.
Upvotes: 0
Views: 1963
Reputation: 35813
Well, as far as I can tell (and it's difficult because the documentation is almost exclusively about query building, with almost nothing about the results) the answer is "this is just how Knex works". It seems like it would make more sense to have the library return the qualified names, but for whatever reason it doesn't.
Instead, if you want to JOIN tables in Knex, it seems that you have to alias (ie. foos.id AS foos_id
) every column that shares a name :(
Upvotes: 2