-
Notifications
You must be signed in to change notification settings - Fork 642
Description
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:
objection.js/lib/queryBuilder/operations/eager/RelationJoinBuilder.js
Lines 607 to 621 in eaab724
| 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.