diff --git a/src/index.js b/src/index.js index 8131117..0f74d41 100644 --- a/src/index.js +++ b/src/index.js @@ -64,14 +64,18 @@ function filterQuery (resourceConfig, params) { if (!joinedTables.some(t => t === relationPath.join('.'))) { let [relation] = localResourceConfig.relationList.filter(r => r.relation === relationName) - let table = getTable(localResourceConfig) - let localId = `${table}.${relation.localKey}` + if (relation) { + let table = getTable(localResourceConfig) + let localId = `${table}.${relation.localKey}` - let relationTable = getTable(relationResourceConfig) - let foreignId = `${relationTable}.${relationResourceConfig.idAttribute}` + let relationTable = getTable(relationResourceConfig) + let foreignId = `${relationTable}.${relationResourceConfig.idAttribute}` - query = query.join(relationTable, localId, foreignId) - joinedTables.push(relationPath.join('.')) + query = query.join(relationTable, localId, foreignId) + joinedTables.push(relationPath.join('.')) + } else { + // local column + } } localResourceConfig = relationResourceConfig } diff --git a/test/findAll.spec.js b/test/findAll.spec.js index 7c9448d..33f9fa7 100644 --- a/test/findAll.spec.js +++ b/test/findAll.spec.js @@ -182,4 +182,14 @@ describe('DSSqlAdapter#findAll', function () { assert.isUndefined(users[0].email); }); + it('should filter when relations have same column if column is qualified', function* () { + var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' }); + var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id}); + + // `id` column must be qualified with `user.` + var users = yield adapter.findAll(User, {'user.id': user1.id, 'profile.email': 'foo@test.com'}); + assert.equal(users.length, 1); + assert.equal(users[0].profileId, profile1.id); + }); + });