Rennzie
Rennzie

Reputation: 212

`leftJoin()` does not return the joined tables data with Objection ORM using Knex.js

leftJoin only returns the first tables data but not the second table when I use the Knex leftJoin() method with an Objection.js model. The same query works fine when I do it directly in the terminal with psql

the query looks like this:

const result = await Table1Model.query()
      .leftJoin(
        'table_2',
        'table_2.table_2_id',
        'table_1.table_2_id'
      )
      .where('table_1_id', '=', table1Id);

I expect the result to include all of the table_2 columns where there is an id match with table_1.

I am only getting columns for table_1

Upvotes: 0

Views: 722

Answers (1)

Fazal Rasel
Fazal Rasel

Reputation: 4526

if you like to get columns only from table_2-

const result = await Table1Model.query()
      .leftJoin(
        'table_2',
        'table_2.table_2_id',
        'table_1.table_2_id'
      )
      .columns('table_2.*') // add columns
      .where('table_1_id', '=', table1Id);

Upvotes: 3

Related Questions