Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Nodejs + Postgresql - how can i handle Insert multi rows query when is duplicate or violates foreign key

I'm using nodejs, express, postgres as sql server. I created Javascript API that sends queries to the server, And i want when i send a multi-line insert query to the server if one or more of the lines already in the system the query will continue without those in the server And if foreign key error happen(is not present in table "tablename") it will ask the client(wait for response) if to add it to the foreign table and then continue with the query.

So far i did this code, but can not figure out where to start with this problem:

insert(){
    return new Promise( async (resolve, reject) => {
        const client = await this.PoolCONN.connect();

        client.query(query, (err, results)=>{
            
            if(err){
                const error = this.HandlError(err)
                client.release();
                return reject(error);
            };
            
            client.release();
            return resolve(results);

        });
    })
}

HandlError(error){
    const ErrorCode = error.code;
    let errorName = "";
    let msg = "";
    switch (ErrorCode) {
        case '23505': // duplicate values
            
            msg = ``;
            errorName = "duplicate";
            break;                
        case '23503': // foreign_key_violation
            msg = ``
            errorName = "foreign key"
            break;                
        default:
            break;
    }

    return {
        error: errorName,
        errorMSG: msg,
    }

}

NOTE: I know I'm asking for a lot I'm a bit lost

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!