Skip to content

Commit

Permalink
fix(belongsTo): use correct FK for the query
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 3, 2019
1 parent 8705df0 commit 0f55910
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Orm/Relations/BelongsTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Binary file added test-helpers/tmp/db.sqlite
Binary file not shown.
75 changes: 75 additions & 0 deletions test/orm/model-belongs-to.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
74 changes: 74 additions & 0 deletions test/orm/model-has-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
75 changes: 75 additions & 0 deletions test/orm/model-has-one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit 0f55910

Please sign in to comment.