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.

Prevent multiple (identical) joins of the same table

See original GitHub issue

I noticed that a new object contains an array for joins, and I was wondering if there was some way to keep track that there would be no duplicate joins. I’m currently building a complicated query builder / api where it’s possible I might join the same table the same way multiple times (which results in a SQL error).

Here’s the situation:

 var db = Knex.initialize({ ..connection stuff ...});  
 var person = db('persons');
 var query = req.query;
 if(typeof query.shoe !== 'undefined') {
    person.join('clothes', 'person.clothes_id', '=', 'clothes.id');
    person.join('shoes', 'clothes.shoe_id', '=', 'shoes.id');
 }
 if(typeof query.shirt !== 'undefined') {
    person.join('clothes', 'person.clothes_id', '=', 'clothes.id');
    person.join('shirts', 'clothes.shirt_id', '=', 'shirts.id');
 }

This would result in an error since we’re joining “clothes” twice. So, I originally tried to create an internal “joined table” collection + a wrapper for the join:

 var person = db('persons');
 person.joinTables = [];
 person.preJoin = function(table, col1, operator, col2){
    if(_.contains(this.joinTables, table)) {
        //make chainable;
        return this;
    }
    else {
        this.joinTables.push(table);
        return this.join(table, col1, operator, col2);
    }
 }

However, I found that while I can easily access the “joinTables” array, the function itself is not accessible. I’m guessing due to the response handler or the way Knex is built with its grammar.js or whatever else.

Any ideas on how I could either implement this function and make it work or have a safety on Knex to allow multiple identical joins? perhaps a check against the joins array to see if an identical entry has been made?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
awesomejerrycommented, Mar 20, 2017

My current workaround is

  if (fieldNeeded['name']) {
    queryBuilder.select('person.display_name as name');
    if (!queryBuilder.personJoined) {
      queryBuilder.join('person', 'staff.person_id', 'person.id');
      queryBuilder.personJoined = true;
    }
  }
  if (fieldNeeded['email']) {
    queryBuilder.select('person.primary_email as email');
    if (!queryBuilder.personJoined) {
      queryBuilder.join('person', 'staff.person_id', 'person.id');
      queryBuilder.personJoined = true;
    }
  }

which is quite verbose…

0reactions
rush-levintcommented, Oct 25, 2018

You may want to utilize and abstract around the builder.queryContext() function found here

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Do You Get Rid of Duplicates in an SQL JOIN?
Stuck with unwanted duplicates from an SQL JOIN? Read this article to understand possible reasons and learn how to fix the query.
Read more >
Getting Duplicate Values When Joining the Same Table Twice
There are many ways to remove duplicates. One of these is to select data with a subselect. Try to put around your select...
Read more >
Prevent multiple (identical) joins of the same table #79 - GitHub
I'm currently building a complicated query builder / api where it's possible I might join the same table the same way multiple times...
Read more >
Avoiding duplicates when joining multiple tables
Try JOINs ; select C.computer_id, sum ; as hdd_capacity, sum ; as ram_capacity from ; left join ; on C.computer_id = ...
Read more >
How To Deal with Duplicate Entries Using SQL
Regardless of how duplicates were created, the first step is to identify them within your data table. The most difficult part here is...
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