@@ -54,8 +54,8 @@ function loadWithRelations (items, resourceConfig, options) {
54
54
task = this . findAll ( resourceConfig . getResource ( relationName ) , {
55
55
where : {
56
56
[ def . foreignKey ] : instance ?
57
- { '==' : instance [ resourceConfig . idAttribute ] } :
58
- { 'in' : items . map ( item => item [ resourceConfig . idAttribute ] ) }
57
+ { '==' : instance [ def . localKey || resourceConfig . idAttribute ] } :
58
+ { 'in' : items . map ( item => item [ def . localKey || resourceConfig . idAttribute ] ) }
59
59
}
60
60
} , __options ) . then ( relatedItems => {
61
61
if ( instance ) {
@@ -66,7 +66,7 @@ function loadWithRelations (items, resourceConfig, options) {
66
66
}
67
67
} else {
68
68
items . forEach ( item => {
69
- let attached = relatedItems . filter ( ri => ri [ def . foreignKey ] === item [ resourceConfig . idAttribute ] )
69
+ let attached = relatedItems . filter ( ri => ri [ def . foreignKey ] === item [ def . localKey || resourceConfig . idAttribute ] )
70
70
if ( def . type === 'hasOne' && attached . length ) {
71
71
item [ def . localField ] = attached [ 0 ]
72
72
} else {
@@ -116,7 +116,12 @@ function loadWithRelations (items, resourceConfig, options) {
116
116
if ( instance ) {
117
117
let id = get ( instance , def . localKey )
118
118
if ( id ) {
119
- task = this . find ( resourceConfig . getResource ( relationName ) , get ( instance , def . localKey ) , __options ) . then ( relatedItem => {
119
+ task = this . findAll ( resourceConfig . getResource ( relationName ) , {
120
+ where : {
121
+ [ def . foreignKey || relationDef . idAttribute ] : { '==' : id }
122
+ }
123
+ } , __options ) . then ( relatedItems => {
124
+ let relatedItem = relatedItems && relatedItems [ 0 ] ;
120
125
instance [ def . localField ] = relatedItem
121
126
return relatedItem
122
127
} )
@@ -126,14 +131,12 @@ function loadWithRelations (items, resourceConfig, options) {
126
131
if ( ids . length ) {
127
132
task = this . findAll ( resourceConfig . getResource ( relationName ) , {
128
133
where : {
129
- [ relationDef . idAttribute ] : {
130
- 'in' : ids
131
- }
134
+ [ def . foreignKey || relationDef . idAttribute ] : { 'in' : ids }
132
135
}
133
136
} , __options ) . then ( relatedItems => {
134
137
items . forEach ( item => {
135
138
relatedItems . forEach ( relatedItem => {
136
- if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
139
+ if ( relatedItem [ def . foreignKey || relationDef . idAttribute ] === item [ def . localKey ] ) {
137
140
item [ def . localField ] = relatedItem
138
141
}
139
142
} )
0 commit comments