@@ -128,6 +128,21 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
128
128
clicks : { type : Sequelize . BIGINT } ,
129
129
} ) ;
130
130
131
+ models . customer = sequelize . define ( 'customer' , {
132
+ name : { type : Sequelize . STRING } ,
133
+ } ) ;
134
+
135
+ models . picture = sequelize . define ( 'picture' , {
136
+ name : { type : Sequelize . STRING } ,
137
+ customerId : {
138
+ type : Sequelize . INTEGER ,
139
+ primaryKey : true ,
140
+ allowNull : false ,
141
+ } ,
142
+ } , {
143
+ underscored : true ,
144
+ } ) ;
145
+
131
146
models . address . belongsTo ( models . user ) ;
132
147
models . addressWithUserAlias . belongsTo ( models . user , { as : 'userAlias' } ) ;
133
148
models . user . hasMany ( models . address ) ;
@@ -152,6 +167,21 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
152
167
sourceKey : 'ownerId' ,
153
168
} ) ;
154
169
170
+ models . customer . hasOne ( models . picture , {
171
+ foreignKey : {
172
+ name : 'customerIdKey' ,
173
+ field : 'customer_id' ,
174
+ } ,
175
+ as : 'picture' ,
176
+ } ) ;
177
+ models . picture . belongsTo ( models . customer , {
178
+ foreignKey : {
179
+ name : 'customerIdKey' ,
180
+ field : 'customer_id' ,
181
+ } ,
182
+ as : 'customer' ,
183
+ } ) ;
184
+
155
185
Interface . Schemas = {
156
186
schemas : {
157
187
user : {
@@ -330,6 +360,26 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
330
360
{ field : 'clicks' , type : 'Number' } ,
331
361
] ,
332
362
} ,
363
+ customer : {
364
+ name : 'owner' ,
365
+ idField : 'id' ,
366
+ primaryKeys : [ 'id' ] ,
367
+ isCompositePrimary : false ,
368
+ fields : [
369
+ { field : 'id' , type : 'Number' } ,
370
+ { field : 'name' , type : 'STRING' } ,
371
+ ] ,
372
+ } ,
373
+ picture : {
374
+ name : 'picture' ,
375
+ idField : 'customerId' ,
376
+ primaryKeys : [ 'customerId' ] ,
377
+ isCompositePrimary : false ,
378
+ fields : [
379
+ { field : 'customerId' , type : 'Number' , reference : 'customer.id' } ,
380
+ { field : 'name' , type : 'STRING' } ,
381
+ ] ,
382
+ } ,
333
383
} ,
334
384
} ;
335
385
@@ -642,6 +692,34 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
642
692
} ) ;
643
693
} ) ;
644
694
695
+ describe ( 'create a record on a collection with a foreign key which is a primary key' , ( ) => {
696
+ it ( 'should create a record' , async ( ) => {
697
+ expect . assertions ( 6 ) ;
698
+ const { models } = initializeSequelize ( ) ;
699
+ try {
700
+ await new ResourceCreator ( models . customer , {
701
+ id : 1 ,
702
+ name : 'foo' ,
703
+ } ) . perform ( ) ;
704
+ const result = await new ResourceCreator ( models . picture , {
705
+ name : 'bar' ,
706
+ customer : 1 ,
707
+ } ) . perform ( ) ;
708
+
709
+ expect ( result . customerId ) . toStrictEqual ( 1 ) ;
710
+ expect ( result . customerIdKey ) . toStrictEqual ( 1 ) ;
711
+ expect ( result . name ) . toStrictEqual ( 'bar' ) ;
712
+
713
+ const picture = await models . picture . findOne ( { where : { name : 'bar' } , include : { model : models . customer , as : 'customer' } } ) ;
714
+ expect ( picture ) . not . toBeNull ( ) ;
715
+ expect ( picture . customerId ) . toStrictEqual ( 1 ) ;
716
+ expect ( picture . customer . id ) . toStrictEqual ( 1 ) ;
717
+ } finally {
718
+ connectionManager . closeConnection ( ) ;
719
+ }
720
+ } ) ;
721
+ } ) ;
722
+
645
723
describe ( 'create a record on a collection with a composite primary key' , ( ) => {
646
724
it ( 'should create a record' , async ( ) => {
647
725
expect . assertions ( 3 ) ;
0 commit comments