Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda