Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda