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

225
Vistas
How in KnexJS to use aliases to avoid the error column reference is ambiguous when filtering from 2 tables

In my backend based on NodeJS, KnexJS, PSQL, I'm having an issue with a filter when from 2 tables is requested to get data using 2 columns which they have the same name.

The issue belongs to this query part and in the specific when is requested in the same time: authorTypes and conversationOriginTypes

The error from PSQL:

column reference "origin_type" is ambiguous
if (!isEmpty(authorTypes) || !isEmpty(authorIds)) {
      this.builder.join(
        memberTable,
        `${memberTable}.${memberColumns.id}`,
        `${tableName}.${columns.memberId}`
      );
      if (!isEmpty(authorTypes)) {
        this.builder.whereIn(memberColumns.memberType, authorTypes);
      }
      if (!isEmpty(authorIds)) {
        this.builder.whereIn(memberColumns.memberId, authorIds);
      }
    }

    if (!isEmpty(conversationOriginTypes) || !isEmpty(conversationOriginIds)) {
      this.builder.join(
        conversationTable,
        `${conversationTable}.${conversationColumns.id}`,
        `${tableName}.${columns.conversationId}`
      );
      if (!isEmpty(conversationOriginTypes)) {
        this.builder.whereIn(
          conversationColumns.originType,
          conversationOriginTypes
        );
      }
      if (!isEmpty(conversationOriginIds)) {
        this.builder.whereIn(
          conversationColumns.originId,
          conversationOriginIds
        );
      }
    }

Both tables has the following structure:

// CONVERSATION TABLE
export const conversationTable = 'conversation';
export const conversationColumns = {
  id: 'id',
  topic: 'topic',
  originType: 'origin_type', -> here the issue!
  originId: 'origin_id',
  createdAt: 'created_at',
};

// MEMBER TABLE
export const memberTable = 'member';
export const memberColumns = {
  id: 'id',
  name: 'name',
  memberType: 'origin_type', -> here the issue!
  memberId: 'origin_id',
  createdAt: 'created_at',
};

I don't know how to modify that part I shared to avoid this issue. I was wondering I need to aliases somehow but I tried some examples online but got the same error so probably I'm doing wrong.

about 4 years ago · Santiago Trujillo
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