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.

Generating select into join clause

See original GitHub issue

I’m getting this SQL when i’m making a join clause:

select 
  "CALL_REQ"."ID", 
  "CALL_REQ"."DESCRIPTION",
  "CUSTOMER"."CONTACT_UUID" "CUSTOMER_CONTACT_UUID", 
  "CUSTOMER"."FIRST_NAME" "CUSTOMER_FIRST_NAME",
  "CUSTOMER"."LAST_NAME" "CUSTOMER_LAST_NAME"
from
  "CALL_REQ"
  inner join (select "CA_CONTACT".* from "CA_CONTACT") "CUSTOMER" on "CUSTOMER"."CONTACT_UUID" = "CALL_REQ"."CUSTOMER"
where "CALL_REQ"."PERSID" = ? and "CALL_REQ"."TYPE" = ?

My db connector is oracle. Here is my code:

// ticket.js
export default class Ticket extends Model {

    static get tableName() {
        return 'CALL_REQ';
    }

    static get idColumn() {
        return 'PERSID';
    }

    static get relationMappings() {
        const Contact =  require('./contact').default;
        return {
            customer: {
                relation: Model.BelongsToOneRelation,
                modelClass: Contact,
                join: {
                    from: `${Contact.tableName}.CONTACT_UUID`,
                    to: `${Ticket.tableName}.CUSTOMER`                    
                }
            }
        }
    }
}
// contact.js
export default class Contact extends Model {

    static get tableName() {
        return 'CA_CONTACT';
    }

    static get idColumn() {
        return 'CONTACT_UUID';
    }
}

And in my query js file i have

let fields = ['CALL_REQ.ID', 'CALL_REQ.DESCRIPTION']
    .concat(['CUSTOMER.CONTACT_UUID as CUSTOMER_CONTACT_UUID', 
    'CUSTOMER.FIRST_NAME as CUSTOMER_FIRST_NAME',
    'CUSTOMER.LAST_NAME as CUSTOMER_LAST_NAME']);
        
return await Ticket.query()
    .findById(id)
    .select(fields)
    .joinRelated('customer', { alias: 'CUSTOMER' })
    .andWhere(Ticket.tableName + '.TYPE', 'R');

What i’ve to do to get the next SQL?

sql
select 
  "CALL_REQ"."ID", 
  "CALL_REQ"."DESCRIPTION",
  "CUSTOMER"."CONTACT_UUID" "CUSTOMER_CONTACT_UUID", 
  "CUSTOMER"."FIRST_NAME" "CUSTOMER_FIRST_NAME",
  "CUSTOMER"."LAST_NAME" "CUSTOMER_LAST_NAME"
from
  "CALL_REQ"
  inner join "CA_CONTACT" "CUSTOMER" on "CUSTOMER"."CONTACT_UUID" = "CALL_REQ"."CUSTOMER"
where "CALL_REQ"."PERSID" = ? and "CALL_REQ"."TYPE" = ?

I’m using objection@2.0.8 Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
moutbcommented, Jan 9, 2020

Hi @koskimas, I just got back from vacation and I haven’t tried it yet. I’d like to do it this weekend.

0reactions
koskimascommented, Jan 9, 2020

@moutb Any luck reproducing this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL SELECT INTO statement
This article covers the SQL SELECT INTO statement including syntax, parameters and use with multiple tables, filegroups and a WHERE ...
Read more >
SQL SELECT INTO Statement - W3Schools
The SELECT INTO statement copies data from one table into a new table. SELECT INTO Syntax. Copy all columns into a new table:...
Read more >
How to use SELECT... INTO with a JOIN? - Stack Overflow
Your PL/SQL is valid and acceptable provided: Table TABLE contains exactly 4 columns, corresponding to the 4 values you are selecting.
Read more >
INTO Clause (Transact-SQL) - SQL Server | Microsoft Learn
The SELECT...INTO statement operates in two parts - the new table is created, and then rows are inserted. This means that if the...
Read more >
SQL joins and how to use them - Launch School
This is done by adding additional JOIN clauses to your SELECT statement. To join multiple tables in this way, there must be a...
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