diff --git a/src/Factory/Relations/ManyToMany.ts b/src/Factory/Relations/ManyToMany.ts index c274f12a..e9a6f0a2 100644 --- a/src/Factory/Relations/ManyToMany.ts +++ b/src/Factory/Relations/ManyToMany.ts @@ -8,7 +8,7 @@ */ import { ManyToManyRelationContract } from '@ioc:Adonis/Lucid/Relations' -import { LucidModel, LucidRow, ModelObject } from '@ioc:Adonis/Lucid/Model' +import { LucidModel, LucidRow } from '@ioc:Adonis/Lucid/Model' import { RelationCallback, FactoryModelContract, @@ -36,18 +36,6 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract public async make(parent: LucidRow, callback?: RelationCallback, count?: number) { const factory = this.compile(callback) const instances = await factory.makeStubbedMany(count || 1) - - const [pivotKey, pivotValue] = this.relation.getPivotPair(parent) - instances.forEach((related) => { - const [pivotRelatedKey, pivotRelatedValue] = this.relation.getPivotRelatedPair(related) - - /** - * Update model $extra properties - */ - related.$extras[pivotKey] = pivotValue - related.$extras[pivotRelatedKey] = pivotRelatedValue - }) - parent.$setRelated(this.relation.relationName, instances) } @@ -58,28 +46,10 @@ export class ManyToMany extends BaseRelation implements FactoryRelationContract const factory = this.compile(callback) const instances = await factory.createMany(count || 1) - /** - * Loop over instances to build pivot attributes - */ - const pivotAttributes: ModelObject = {} - const [pivotKey, pivotValue] = this.relation.getPivotPair(parent) - instances.forEach((related) => { - const [pivotRelatedKey, pivotRelatedValue] = this.relation.getPivotRelatedPair(related) - - /** - * Update model $extra properties - */ - related.$extras[pivotKey] = pivotValue - related.$extras[pivotRelatedKey] = pivotRelatedValue - - // custom pivot attributes will come here - pivotAttributes[pivotRelatedValue] = {} - }) - /** * Make pivot insert query */ - await this.relation.client(parent, this.ctx.$trx!).attach(pivotAttributes) + await this.relation.client(parent, this.ctx.$trx!).saveMany(instances) /** * Setup in-memory relationship diff --git a/test/factory/many-to-many.spec.ts b/test/factory/many-to-many.spec.ts index f3d9b59c..b9711451 100644 --- a/test/factory/many-to-many.spec.ts +++ b/test/factory/many-to-many.spec.ts @@ -103,10 +103,7 @@ test.group('Factory | ManyToMany | make', (group) => { assert.exists(user.skills[0].id) assert.instanceOf(user.skills[0], Skill) - assert.deepEqual(user.skills[0].$extras, { - skill_id: user.skills[0].id, - user_id: user.id, - }) + assert.deepEqual(user.skills[0].$extras, {}) assert.isFalse(user.skills[0].$isPersisted) }) @@ -165,10 +162,6 @@ test.group('Factory | ManyToMany | make', (group) => { assert.instanceOf(user.skills[0], Skill) assert.isFalse(user.skills[0].$isPersisted) assert.equal(user.skills[0].name, 'Dancing') - assert.deepEqual(user.skills[0].$extras, { - skill_id: user.skills[0].id, - user_id: user.id, - }) }) test('make many relationship', async (assert) => { @@ -231,16 +224,6 @@ test.group('Factory | ManyToMany | make', (group) => { assert.instanceOf(user.skills[1], Skill) assert.isFalse(user.skills[1].$isPersisted) assert.equal(user.skills[1].name, 'Dancing') - - assert.deepEqual(user.skills[0].$extras, { - skill_id: user.skills[0].id, - user_id: user.id, - }) - - assert.deepEqual(user.skills[1].$extras, { - skill_id: user.skills[1].id, - user_id: user.id, - }) }) }) @@ -312,10 +295,6 @@ test.group('Factory | ManyToMany | create', (group) => { assert.lengthOf(user.skills, 1) assert.instanceOf(user.skills[0], Skill) assert.isTrue(user.skills[0].$isPersisted) - assert.deepEqual(user.skills[0].$extras, { - user_id: user.id, - skill_id: user.skills[0].id, - }) const users = await db.from('users').select('*') const skills = await db.from('skills').select('*') @@ -380,10 +359,6 @@ test.group('Factory | ManyToMany | create', (group) => { assert.instanceOf(user.skills[0], Skill) assert.isTrue(user.skills[0].$isPersisted) assert.equal(user.skills[0].name, 'Dancing') - assert.deepEqual(user.skills[0].$extras, { - user_id: user.id, - skill_id: user.skills[0].id, - }) }) test('create many relationships', async (assert) => { @@ -439,18 +414,10 @@ test.group('Factory | ManyToMany | create', (group) => { assert.instanceOf(user.skills[0], Skill) assert.isTrue(user.skills[0].$isPersisted) assert.equal(user.skills[0].name, 'Dancing') - assert.deepEqual(user.skills[0].$extras, { - user_id: user.id, - skill_id: user.skills[0].id, - }) assert.instanceOf(user.skills[1], Skill) assert.isTrue(user.skills[1].$isPersisted) assert.equal(user.skills[1].name, 'Programming') - assert.deepEqual(user.skills[1].$extras, { - user_id: user.id, - skill_id: user.skills[1].id, - }) }) test('rollback changes on error', async (assert) => {