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.