Skip to content

Ambiguous column mapping for RelationJoinBuilder (no table ref) #844

@ferk6a

Description

@ferk6a

Hello! I'm working with a schema that has a lot of columns with identical names, and when doing a leftJoin query I came accross the identity column (which is a composite key) being selected ambiguously with another table (I'm doing weird things like joining tables on filters). I found the culprit as being the function createRelatedJoinFromQuery after debugging a little bit:

function findAllForeignKeysForModel({ modelClass, allRelations }) {
const foreignKeys = modelClass.getIdColumnArray().slice();
allRelations.forEach(rel => {
if (rel.relatedModelClass === modelClass) {
rel.relatedProp.cols.forEach(col => foreignKeys.push(col));
}
if (rel.ownerModelClass === modelClass) {
rel.ownerProp.cols.forEach(col => foreignKeys.push(col));
}
});
return uniq(foreignKeys);
}

As you can see, the columns are being appended without any table reference, they are being selected purely by their names. To fix my issue, I ended up changing findAllForeignKeysForModel to this:

function findAllForeignKeysForModel({ modelClass, allRelations }) {
  const foreignKeys = modelClass.getIdColumnArray().map(col => `${modelClass.getTableName()}.${col}`);

  allRelations.forEach(rel => {
    if (rel.relatedModelClass === modelClass) {
      rel.relatedProp.cols.forEach(col => foreignKeys.push(`${modelClass.getTableName()}.${col}`));
    }

    if (rel.ownerModelClass === modelClass) {
      rel.ownerProp.cols.forEach(col => foreignKeys.push(`${modelClass.getTableName()}.${col}`));
    }
  });

  return uniq(foreignKeys);
}

Which to be honest, isn't a perfect solution, but it does work perfectly with my complex queries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions