@@ -294,45 +294,44 @@ class DSSqlAdapter {
294294
295295 while ( parts . length >= 2 ) {
296296 let relationName = parts . shift ( )
297- let relationResourceConfig = resourceConfig . getResource ( relationName )
298- relationPath . push ( relationName )
299-
300- if ( localResourceConfig . relationList ) {
301- let [ relation ] = localResourceConfig . relationList . filter ( r => r . relation === relationName )
302- if ( relation ) {
303- if ( relation . type === 'belongsTo' || relation . type === 'hasOne' ) {
304- // Apply table join for belongsTo/hasOne property (if not done already)
305- if ( ! joinedTables . some ( t => t === relationPath . join ( '.' ) ) ) {
306- let table = getTable ( localResourceConfig )
307- let localId = `${ table } .${ relation . localKey } `
308-
309- let relationTable = getTable ( relationResourceConfig )
310- let foreignId = `${ relationTable } .${ relationResourceConfig . idAttribute } `
311-
312- query . join ( relationTable , localId , foreignId )
313- joinedTables . push ( relationPath . join ( '.' ) )
314- }
315- } else if ( relation . type === 'hasMany' ) {
316- // Perform `WHERE EXISTS` subquery for hasMany property
297+ let [ relation ] = localResourceConfig . relationList . filter ( r => r . relation === relationName || r . localField === relationName )
298+
299+ if ( relation ) {
300+ let relationResourceConfig = resourceConfig . getResource ( relation . relation )
301+ relationPath . push ( relation . relation )
302+
303+ if ( relation . type === 'belongsTo' || relation . type === 'hasOne' ) {
304+ // Apply table join for belongsTo/hasOne property (if not done already)
305+ if ( ! joinedTables . some ( t => t === relationPath . join ( '.' ) ) ) {
317306 let table = getTable ( localResourceConfig )
318- let localId = `${ table } .${ localResourceConfig . idAttribute } `
307+ let localId = `${ table } .${ relation . localKey } `
319308
320309 let relationTable = getTable ( relationResourceConfig )
321- let foreignId = `${ relationTable } .${ relation . foreignKey } `
322-
323- let existsParams = {
324- [ foreignId ] : { '===' : knex . raw ( localId ) } ,
325- [ parts [ 0 ] ] : criteria
326- } ;
327- query . whereExists ( this . filterQuery ( relationResourceConfig , existsParams , options ) ) ;
328- criteria = null ; // criteria handled by EXISTS subquery
310+ let foreignId = `${ relationTable } .${ relationResourceConfig . idAttribute } `
311+
312+ query . join ( relationTable , localId , foreignId )
313+ joinedTables . push ( relationPath . join ( '.' ) )
329314 }
330- } else {
331- // hopefully a qualified local column
315+ } else if ( relation . type === 'hasMany' ) {
316+ // Perform `WHERE EXISTS` subquery for hasMany property
317+ let table = getTable ( localResourceConfig )
318+ let localId = `${ table } .${ localResourceConfig . idAttribute } `
319+
320+ let relationTable = getTable ( relationResourceConfig )
321+ let foreignId = `${ relationTable } .${ relation . foreignKey } `
322+
323+ let existsParams = {
324+ [ foreignId ] : { '===' : knex . raw ( localId ) } ,
325+ [ parts [ 0 ] ] : criteria
326+ } ;
327+ query . whereExists ( this . filterQuery ( relationResourceConfig , existsParams , options ) ) ;
328+ criteria = null ; // criteria handled by EXISTS subquery
332329 }
333330
334331 localResourceConfig = relationResourceConfig
335- }
332+ } else {
333+ // hopefully a qualified local column
334+ }
336335 }
337336
338337 return `${ getTable ( localResourceConfig ) } .${ parts [ 0 ] } `
0 commit comments