Closed k98kumar closed 5 years ago
I just realized that I can use eager loading to get all the data I'm looking for without doing joins myself. However, I am still wondering why the leftJoin
and leftJoinRelation
methods did not work and knex.raw()
worked.
Joins don't nest the objects like eager loading. They simply do an SQL join.
I understand that, but it seems like the leftJoin()
and leftJoinRelation()
don't do anything.
When I do curl http://localhost:8000/test/1
:
For the leftJoin
s, I get:
[ User { id: 1, username: 'username', password: 'password' } ]
But for knex.raw()
, I get:
[
{
id: 1,
username: 'username',
password: 'password',
user_id: 1,
title: 'Shopping List',
document_id: '012023b2-30e9-4cff-87af-0c154ca0b2c9',
document_type: 'shoppingList',
name: 'shoppingList-ingredient'
}
]
@koskimas Is there a reason that leftJoin
doesn't actually do the SQL join like knex.raw
?
The join is made, but the fields aren't included. You have to manually .columns(['origin.','related.'] to include the fields in your query. You can enable debug logging and view the query that is actually made to see this behavior.
How the db is set up:
I am having an issue with the file at /src/routes/test.js. First step is to do the post:
curl -X POST -H "Content-Type:application/json" http://localhost:8000/test
. In therouter.get('/:userId', async(req, res) => {})
, the leftJoins won't work. Also, when I replace the two leftJoin calls with the leftJoinRelation call, the same thing is logged. However, with thereq.context.models.User.knex().raw()
the join works.Question: Am I forgetting to do a step before joining?
Also, is there a method to output the data as a JSON object, like the one we put into insertGraph? Currently, I am just usingknex.raw()
and I'm receiving an array of row objects.DB: Postgres
/db/migrations/schema.js
/src/index.js
/src/models/index.js
/src/models/Ingredient.js
/src/models/Recipe.js
/src/models/ShoppingList.js
/src/models/User.js
/src/routes/test.js