Skip to content

Commit 9893ed1

Browse files
committed
fix: Push related records when creating relationships via factory
Closes: #1100
1 parent d25eeec commit 9893ed1

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/factories/relations/has_many.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class HasMany extends BaseRelation implements FactoryRelationContract {
4545
})
4646
.makeStubbedMany(count || 1)
4747

48-
parent.$setRelated(this.relation.relationName, instances)
48+
parent.$pushRelated(this.relation.relationName, instances)
4949
}
5050

5151
/**
@@ -63,6 +63,6 @@ export class HasMany extends BaseRelation implements FactoryRelationContract {
6363
})
6464
.createMany(count || 1)
6565

66-
parent.$setRelated(this.relation.relationName, instance)
66+
parent.$pushRelated(this.relation.relationName, instance)
6767
}
6868
}

src/factories/relations/many_to_many.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract
3939
async make(parent: LucidRow, callback?: RelationCallback, count?: number) {
4040
const builder = this.compile(this, parent, callback)
4141
const instances = await builder.makeStubbedMany(count || 1)
42-
parent.$setRelated(this.relation.relationName, instances)
42+
parent.$pushRelated(this.relation.relationName, instances)
4343
}
4444

4545
/**
@@ -81,6 +81,6 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract
8181
/**
8282
* Setup in-memory relationship
8383
*/
84-
parent.$setRelated(this.relation.relationName, instances)
84+
parent.$pushRelated(this.relation.relationName, instances)
8585
}
8686
}

test/factory/has_many.spec.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,85 @@ test.group('Factory | HasMany | create', (group) => {
478478
assert.equal(user.posts[1].title, 'Lucid 101')
479479
})
480480

481+
test('create many relationship with different states', async ({ fs, assert }) => {
482+
const app = new AppFactory().create(fs.baseUrl, () => {})
483+
await app.init()
484+
const db = getDb()
485+
const adapter = ormAdapter(db)
486+
const BaseModel = getBaseModel(adapter)
487+
488+
class Post extends BaseModel {
489+
@column()
490+
declare userId: number
491+
492+
@column()
493+
declare tenantId: number
494+
495+
@column()
496+
declare title: string
497+
}
498+
Post.boot()
499+
500+
class User extends BaseModel {
501+
@column({ isPrimary: true })
502+
declare id: number
503+
504+
@column()
505+
declare username: string
506+
507+
@column()
508+
points: number = 0
509+
510+
@hasMany(() => Post)
511+
declare posts: HasMany<typeof Post>
512+
}
513+
514+
const postFactory = factoryManager
515+
.define(Post, () => {
516+
return {
517+
title: 'Adonis 101',
518+
}
519+
})
520+
.build()
521+
522+
const factory = factoryManager
523+
.define(User, () => {
524+
return {}
525+
})
526+
.relation('posts', () => postFactory)
527+
.build()
528+
529+
const user = await factory
530+
.with('posts', 2, (related) => related.merge({ title: 'Lucid 101' }))
531+
.with('posts', 2, (related) => related.merge({ title: 'Lucid 101', tenantId: 1 }))
532+
.create()
533+
534+
assert.isTrue(user.$isPersisted)
535+
assert.lengthOf(user.posts, 4)
536+
537+
assert.instanceOf(user.posts[0], Post)
538+
assert.isTrue(user.posts[0].$isPersisted)
539+
assert.equal(user.posts[0].userId, user.id)
540+
assert.equal(user.posts[0].title, 'Lucid 101')
541+
542+
assert.instanceOf(user.posts[1], Post)
543+
assert.isTrue(user.posts[1].$isPersisted)
544+
assert.equal(user.posts[1].userId, user.id)
545+
assert.equal(user.posts[1].title, 'Lucid 101')
546+
547+
assert.instanceOf(user.posts[2], Post)
548+
assert.isTrue(user.posts[2].$isPersisted)
549+
assert.equal(user.posts[2].userId, user.id)
550+
assert.equal(user.posts[2].tenantId, 1)
551+
assert.equal(user.posts[2].title, 'Lucid 101')
552+
553+
assert.instanceOf(user.posts[3], Post)
554+
assert.isTrue(user.posts[3].$isPersisted)
555+
assert.equal(user.posts[3].userId, user.id)
556+
assert.equal(user.posts[3].tenantId, 1)
557+
assert.equal(user.posts[3].title, 'Lucid 101')
558+
})
559+
481560
test('create relationship with custom foreign key', async ({ fs, assert }) => {
482561
const app = new AppFactory().create(fs.baseUrl, () => {})
483562
await app.init()

0 commit comments

Comments
 (0)