@@ -33,6 +33,16 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
33
33
callback ?: ( factory : any ) => void ,
34
34
} [ ] = [ ]
35
35
36
+ /**
37
+ * Belongs to relationships are treated different, since they are
38
+ * persisted before the parent model
39
+ */
40
+ private withBelongsToRelations : {
41
+ name : string ,
42
+ count ?: number ,
43
+ callback ?: ( factory : any ) => void ,
44
+ } [ ] = [ ]
45
+
36
46
/**
37
47
* The current index. Updated by `makeMany` and `createMany`
38
48
*/
@@ -151,8 +161,14 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
151
161
/**
152
162
* Makes and persists relationship instances
153
163
*/
154
- public async createRelations ( modelInstance : LucidRow , ctx : FactoryContextContract ) {
155
- for ( let { name, count, callback } of this . withRelations ) {
164
+ private async createRelations (
165
+ modelInstance : LucidRow ,
166
+ ctx : FactoryContextContract ,
167
+ cycle : 'before' | 'after' ,
168
+ ) {
169
+ const relationships = cycle === 'before' ? this . withBelongsToRelations : this . withRelations
170
+
171
+ for ( let { name, count, callback } of relationships ) {
156
172
const relation = this . model . getRelation ( name )
157
173
await relation . useCtx ( ctx ) . create ( modelInstance , callback , count )
158
174
}
@@ -170,8 +186,15 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
170
186
/**
171
187
* Load relationship
172
188
*/
173
- public with ( relation : string , count ?: number , callback ?: ( factory : never ) => void ) : this {
174
- this . withRelations . push ( { name : relation , count, callback } )
189
+ public with ( name : string , count ?: number , callback ?: ( factory : never ) => void ) : this {
190
+ const relation = this . model . getRelation ( name )
191
+
192
+ if ( relation . relation . type === 'belongsTo' ) {
193
+ this . withBelongsToRelations . push ( { name, count, callback } )
194
+ return this
195
+ }
196
+
197
+ this . withRelations . push ( { name, count, callback } )
175
198
return this
176
199
}
177
200
@@ -250,16 +273,24 @@ export class FactoryBuilder implements FactoryBuilderContract<FactoryModelContra
250
273
await this . model . hooks . exec ( 'before' , 'create' , this , modelInstance , ctx )
251
274
252
275
try {
276
+ modelInstance . $trx = ctx . $trx
277
+
278
+ /**
279
+ * Create belongs to relationships before calling the save method. Even though
280
+ * we can update the foriegn key after the initial insert call, we avoid it
281
+ * for cases, where FK is a not nullable.
282
+ */
283
+ await this . createRelations ( modelInstance , ctx , 'before' )
284
+
253
285
/**
254
286
* Persist model instance
255
287
*/
256
- modelInstance . $trx = ctx . $trx
257
288
await modelInstance . save ( )
258
289
259
290
/**
260
291
* Create relationships.
261
292
*/
262
- await this . createRelations ( modelInstance , ctx )
293
+ await this . createRelations ( modelInstance , ctx , 'after' )
263
294
264
295
/**
265
296
* Fire after hook before the transaction is committed, so that
0 commit comments