question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Eager loading: select all rows without association in mn-table

See original GitHub issue

Again, the eager loading is bugging me and I can’t figure how to achieve a select which selects all rows from given table, which do NOT join on a second model.

In pure SQL, I’d write my query like this:


SELECT column1 from Parameters LEFT OUTER JOIN ParameterProjects ON (Project.id = ParameterProjects.parameter_id) WHERE ParameterProjects.parameter_id IS NULL;

It’s usually a quite trivial task to fetch all rows which are not (yet) linked to table#2 (Projects). In sequelize however I the only way I know to get the mn-table involved is to include the associated model. My first idea was to use Sequelize.or() (which is technically working now, thanks Mick!) and query for Sequelize.or({project_id: 1},{project_id:null}) , but I didn’t pay enough attention at the generated query.

It looks like this:

SELECT `Parameters` . * , `Projects`.`id` AS `Projects.id` , `Projects`.`name` AS `Projects.name` , `Projects`.`url` AS `Projects.url` , `Projects`.`api_key` AS `Projects.api_key` , `Projects`.`created_at` AS `Projects.created_at` , `Projects`.`customer_id` AS `Projects.customer_id` , `Projects.ParametersProject`.`project_id` AS `Projects.ParametersProject.project_id` , `Projects.ParametersProject`.`parameter_id` AS `Projects.ParametersProject.parameter_id`
FROM `Parameters`
INNER JOIN `ParametersProjects` AS `Projects.ParametersProject` ON `Parameters`.`id` = `Projects.ParametersProject`.`parameter_id`
INNER JOIN `Projects` AS `Projects` ON `Projects`.`id` = `Projects.ParametersProject`.`project_id`
AND (
`Projects`.`id` = '1'
OR `Projects`.`id` IS NULL
)
WHERE (
`Parameters`.`key` = 'cpu'
)

Table Parameters joins ParameterProjects on the Project.id, ParameterProjects joins Projects. Those are INNER JOINS and as far as I understand SQL (please correct if I’m wrong) Projects.ParametersProject.parameter_id can never be NULL.

Is there perhaps another way to achieve this?

Oh, and btw: Where are the fancy subselects gone? I could swear the last time I tried to solve my issue the queries had subselects in it.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
kukukkcommented, Jun 2, 2018

I’m also interested how can this be solved in version 4. Using required:false returns all rows (with and without having association).

5reactions
janmeiercommented, Nov 17, 2015

@YouriT You should be able to use where: { '$portfolios.userId$': null }, to avoid prepending the parent name as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

select specific columns from first table with eager loading in ...
for example: I want specific columns from model User and want to get relationship data with eager loading without any join. $users= User::select...
Read more >
module Sequel::Model::Associations::DatasetMethods
Eager loading makes it so that you can load all associated records for a set of objects in a single query, instead of...
Read more >
PHP Laravel Eloquent select specific columns ... - YouTube
... only specific columns while eager loading. This is useful in cases where you have nested relationship and you don't need to select...
Read more >
Optimize Laravel Eloquent Queries with Eager Loading
In this tutorial, we will set up some example relationships and then walk through how queries change with and without eager loading.
Read more >
Laravel — Eager Load only one record for one-to-many ...
We can still modify the relations and eager-load them, as long as we use query builder method within the relationship call.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found