diff --git a/src/Orm/Relations/BelongsTo.ts b/src/Orm/Relations/BelongsTo.ts index c7c3aba1..9a8c8efe 100644 --- a/src/Orm/Relations/BelongsTo.ts +++ b/src/Orm/Relations/BelongsTo.ts @@ -194,19 +194,10 @@ export class BelongsTo implements RelationContract { * Must be implemented by parent class */ public setRelatedMany (models: ModelContract[], related: ModelContract[]) { - /** - * Instead of looping over the model instances, we loop over the related model instances, since - * it can improve performance in some case. For example: - * - * - There are 10 parentInstances and we all of them to have one related instance, in - * this case we run 10 iterations. - * - There are 10 parentInstances and 8 of them have related instance, in this case we run 8 - * iterations vs 10. - */ - related.forEach((one) => { - const relation = models.find((model) => model[this.foreignKey] === one[this.localKey]) + models.forEach((model) => { + const relation = related.find((one) => one[this.localKey] === model[this.foreignKey]) if (relation) { - this.setRelated(relation, one) + this.setRelated(model, relation) } }) } diff --git a/test-helpers/tmp/db.sqlite b/test-helpers/tmp/db.sqlite new file mode 100644 index 00000000..4e86a0d5 Binary files /dev/null and b/test-helpers/tmp/db.sqlite differ diff --git a/test/orm/model-belongs-to.spec.ts b/test/orm/model-belongs-to.spec.ts index 70ec6ed3..78af8359 100644 --- a/test/orm/model-belongs-to.spec.ts +++ b/test/orm/model-belongs-to.spec.ts @@ -174,10 +174,53 @@ test.group('Model | Belongs To', (group) => { public user: User } + await db.insertQuery().table('users').insert([{ username: 'virk' }]) + + const users = await db.query().from('users') + await db.insertQuery().table('profiles').insert([ + { + user_id: users[0].id, + display_name: 'virk', + }, + { + user_id: users[0].id, + display_name: 'virk', + }, + ]) + + const profiles = await Profile.query().preload('user') + assert.instanceOf(profiles[0].user, User) + assert.instanceOf(profiles[1].user, User) + }) + + test('preload belongsTo relationship for many rows', async (assert) => { + class User extends BaseModel { + @column({ primary: true }) + public id: number + } + + class Profile extends BaseModel { + @column({ primary: true }) + public id: number + + @column() + public userId: number + + @column() + public displayName: string + + @belongsTo(() => User) + public user: User + } + await db.insertQuery().table('users').insert([{ username: 'virk' }, { username: 'nikk' }]) const users = await db.query().from('users') await db.insertQuery().table('profiles').insert([ + { + user_id: users[0].id, + display_name: 'virk', + }, { user_id: users[0].id, display_name: 'virk', @@ -188,8 +231,10 @@ test.group('Model | Belongs To', (group) => { }, ]) - const profile = await Profile.query().preload('user').where('display_name', 'virk').first() - assert.instanceOf(profile!.user, User) + const profiles = await Profile.query().preload('user') + assert.equal(profiles[0].user.id, 1) + assert.equal(profiles[1].user.id, 1) + assert.equal(profiles[2].user.id, 2) }) test('raise exception when foreign key is not selected', async (assert) => {