diff --git a/src/Orm/Relations/BelongsTo.ts b/src/Orm/Relations/BelongsTo.ts index b14eaf56..7c3da963 100644 --- a/src/Orm/Relations/BelongsTo.ts +++ b/src/Orm/Relations/BelongsTo.ts @@ -173,7 +173,7 @@ export class BelongsTo implements BaseRelationContract { */ public getEagerQuery (parents: ModelContract[], client: QueryClientContract) { const values = uniq(parents.map((parentInstance) => { - return this._ensureValue(parentInstance[this.localKey]) + return this._ensureValue(parentInstance[this.foreignKey]) })) return this.relatedModel() diff --git a/test-helpers/tmp/db.sqlite b/test-helpers/tmp/db.sqlite new file mode 100644 index 00000000..cefd695c 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 7a4092fe..1545bb94 100644 --- a/test/orm/model-belongs-to.spec.ts +++ b/test/orm/model-belongs-to.spec.ts @@ -165,6 +165,81 @@ test.group('Model | Belongs To', (group) => { assert.equal(Profile.$getRelation('user')!['foreignAdapterKey'], 'user_id') }) + test('get eager query', (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 + } + + Profile.$getRelation('user')!.boot() + const profile = new Profile() + profile.userId = 1 + + const { sql, bindings } = Profile.$getRelation('user')! + .getEagerQuery([profile], Profile.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('users') + .whereIn('id', [1]) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + + test('get query', (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 + } + + Profile.$getRelation('user')!.boot() + const profile = new Profile() + profile.userId = 1 + + const { sql, bindings } = Profile.$getRelation('user')! + .getQuery(profile, Profile.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('users') + .where('id', 1) + .limit(1) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + test('preload belongsTo relationship', async (assert) => { class User extends BaseModel { @column({ primary: true }) diff --git a/test/orm/model-has-many.spec.ts b/test/orm/model-has-many.spec.ts index f5511e56..a9891ca4 100644 --- a/test/orm/model-has-many.spec.ts +++ b/test/orm/model-has-many.spec.ts @@ -167,6 +167,80 @@ test.group('Model | Has Many', (group) => { assert.equal(User.$getRelation('posts')!['foreignAdapterKey'], 'user_id') }) + test('get eager query', (assert) => { + class Post extends BaseModel { + @column({ primary: true }) + public id: number + + @column() + public userId: number + + @column() + public title: string + } + + class User extends BaseModel { + @column({ primary: true }) + public id: number + + @hasMany(() => Post) + public posts: Post[] + } + + User.$getRelation('posts')!.boot() + const user = new User() + user.id = 1 + + const { sql, bindings } = User.$getRelation('posts')! + .getEagerQuery([user], User.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('posts') + .whereIn('user_id', [1]) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + + test('get query', (assert) => { + class Post extends BaseModel { + @column({ primary: true }) + public id: number + + @column() + public userId: number + + @column() + public title: string + } + + class User extends BaseModel { + @column({ primary: true }) + public id: number + + @hasMany(() => Post) + public posts: Post[] + } + + User.$getRelation('posts')!.boot() + const user = new User() + user.id = 1 + + const { sql, bindings } = User.$getRelation('posts')! + .getQuery(user, User.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('posts') + .where('user_id', 1) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + test('preload has many relationship', async (assert) => { class Post extends BaseModel { @column({ primary: true }) diff --git a/test/orm/model-has-one.spec.ts b/test/orm/model-has-one.spec.ts index 35d17a36..55f09954 100644 --- a/test/orm/model-has-one.spec.ts +++ b/test/orm/model-has-one.spec.ts @@ -171,6 +171,81 @@ test.group('Model | Has one', (group) => { assert.equal(User.$getRelation('profile')!['foreignAdapterKey'], 'user_id') }) + test('get eager query', (assert) => { + class Profile extends BaseModel { + @column({ primary: true }) + public id: number + + @column() + public userId: number + + @column() + public displayName: string + } + + class User extends BaseModel { + @column({ primary: true }) + public id: number + + @hasOne(() => Profile) + public profile: Profile + } + + User.$getRelation('profile')!.boot() + const user = new User() + user.id = 1 + + const { sql, bindings } = User.$getRelation('profile')! + .getEagerQuery([user], User.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('profiles') + .whereIn('user_id', [1]) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + + test('get query', (assert) => { + class Profile extends BaseModel { + @column({ primary: true }) + public id: number + + @column() + public userId: number + + @column() + public displayName: string + } + + class User extends BaseModel { + @column({ primary: true }) + public id: number + + @hasOne(() => Profile) + public profile: Profile + } + + User.$getRelation('profile')!.boot() + const user = new User() + user.id = 1 + + const { sql, bindings } = User.$getRelation('profile')! + .getQuery(user, User.query().client) + .toSQL() + + const { sql: knexSql, bindings: knexBindings } = db.query() + .from('profiles') + .where('user_id', 1) + .limit(1) + .toSQL() + + assert.equal(sql, knexSql) + assert.deepEqual(bindings, knexBindings) + }) + test('preload has one relationship', async (assert) => { class Profile extends BaseModel { @column({ primary: true })