@@ -49,6 +49,7 @@ module.exports = class BelongsToRelationGenerator extends (
4949 ) ;
5050
5151 this . artifactInfo . relationPropertyName = options . relationName ;
52+ this . artifactInfo . keyTo = options . keyTo ;
5253 this . artifactInfo . targetModelPrimaryKey =
5354 options . destinationModelPrimaryKey ;
5455 this . artifactInfo . targetModelPrimaryKeyType =
@@ -85,7 +86,11 @@ module.exports = class BelongsToRelationGenerator extends (
8586 const relationName = options . relationName ;
8687 const defaultRelationName = options . defaultRelationName ;
8788 const foreignKeyName = options . foreignKeyName ;
88- const fktype = options . destinationModelPrimaryKeyType ;
89+ const keyTo = options . keyTo ;
90+ const fktype = keyTo
91+ ? relationUtils . getModelPropertyType ( modelDir , targetModel , keyTo ) ||
92+ options . destinationModelPrimaryKeyType
93+ : options . destinationModelPrimaryKeyType ;
8994
9095 const project = new relationUtils . AstLoopBackProject ( ) ;
9196 const sourceFile = relationUtils . addFileToProject (
@@ -103,6 +108,7 @@ module.exports = class BelongsToRelationGenerator extends (
103108 defaultRelationName ,
104109 foreignKeyName ,
105110 fktype ,
111+ keyTo ,
106112 ) ;
107113
108114 relationUtils . addProperty ( sourceClass , modelProperty ) ;
@@ -123,20 +129,29 @@ module.exports = class BelongsToRelationGenerator extends (
123129 defaultRelationName ,
124130 foreignKeyName ,
125131 fktype ,
132+ keyTo ,
126133 ) {
134+ const keyToOption = keyTo ? `, keyTo: '${ keyTo } '` : '' ;
135+
127136 // checks if relation name is customized
128137 let relationDecorator = [
129138 {
130139 name : 'belongsTo' ,
131- arguments : [ `() => ${ className } ` ] ,
140+ arguments : [
141+ keyTo
142+ ? `() => ${ className } , {keyTo: '${ keyTo } '}`
143+ : `() => ${ className } ` ,
144+ ] ,
132145 } ,
133146 ] ;
134147 // already checked if the relation name is the same as the source key before
135148 if ( defaultRelationName !== relationName ) {
136149 relationDecorator = [
137150 {
138151 name : 'belongsTo' ,
139- arguments : [ `() => ${ className } , {name: '${ relationName } '}` ] ,
152+ arguments : [
153+ `() => ${ className } , {name: '${ relationName } '${ keyToOption } }` ,
154+ ] ,
140155 } ,
141156 ] ;
142157 }
0 commit comments