@@ -149,9 +149,19 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
149149
150150 options . __merge = {
151151 from : { table : association . mergeTable , field : Object . keys ( association . mergeAssocId ) } ,
152- to : { table : association . model . table , field : association . model . id } ,
152+ to : { table : association . model . table , field : association . model . id . slice ( 0 ) } , // clone model id
153153 where : [ association . mergeTable , { } ]
154154 } ;
155+
156+ // Loop through the (cloned) association model id fields ... some of them may've been mapped to different
157+ // names in the actual database - if so update to the mapped database column name
158+ for ( i = 0 ; i < options . __merge . to . field . length ; i ++ ) {
159+ var idProp = association . model . properties [ options . __merge . to . field [ i ] ] ;
160+ if ( idProp && idProp . mapsTo ) {
161+ options . __merge . to . field [ i ] = idProp . mapsTo ;
162+ }
163+ }
164+
155165 options . extra = association . props ;
156166 options . extra_info = {
157167 table : association . mergeTable ,
@@ -231,12 +241,22 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
231241
232242 options . __merge = {
233243 from : { table : association . mergeTable , field : Object . keys ( association . mergeAssocId ) } ,
234- to : { table : association . model . table , field : association . model . id } ,
244+ to : { table : association . model . table , field : association . model . id . slice ( 0 ) } , // clone model id
235245 where : [ association . mergeTable , { } ]
236246 } ;
237- options . extra = association . props ;
238- options . extra_info = {
239- table : association . mergeTable ,
247+
248+ // Loop through the (cloned) association model id fields ... some of them may've been mapped to different
249+ // names in the actual database - if so update to the mapped database column name
250+ for ( i = 0 ; i < options . __merge . to . field . length ; i ++ ) {
251+ var idProp = association . model . properties [ options . __merge . to . field [ i ] ] ;
252+ if ( idProp && idProp . mapsTo ) {
253+ options . __merge . to . field [ i ] = idProp . mapsTo ;
254+ }
255+ }
256+
257+ options . extra = association . props ;
258+ options . extra_info = {
259+ table : association . mergeTable ,
240260 id : util . values ( Instance , Model . id ) ,
241261 id_prop : Object . keys ( association . mergeId ) ,
242262 assoc_prop : Object . keys ( association . mergeAssocId )
0 commit comments