Skip to content

Commit

Permalink
fix: belongsToMany is return [] if nothing references records
Browse files Browse the repository at this point in the history
  • Loading branch information
kkyouhei committed Feb 17, 2019
1 parent 07e066c commit e30347d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/attributes/relations/BelongsToMany.ts
Expand Up @@ -112,7 +112,7 @@ export default class BelongsToMany extends Relation {
collection.forEach((item) => {
const related = relateds[item[this.parentKey]]

item[name] = related
item[name] = related || []
})
}

Expand Down
4 changes: 3 additions & 1 deletion test/feature/relations/BelongsToMany_Retrieve.spec.js
Expand Up @@ -248,7 +248,7 @@ describe('Feature – Relations – Belongs To Many – Retrieve', () => {
createStore([{ model: User }, { model: Role }, { model: RoleUser }])

await User.create({
data: { id: 1 }
data: [ { id: 1 }, { id: 2} ]
})

await Role.create({
Expand All @@ -264,5 +264,7 @@ describe('Feature – Relations – Belongs To Many – Retrieve', () => {
expect(users[0].id).toBe(1)
expect(users[0].roles.length).toBe(1)
expect(users[0].roles[0].id).toBe(1)

expect(users[1].roles.length).toBe(0)
})
})

0 comments on commit e30347d

Please sign in to comment.