Skip to content

Commit

Permalink
fix: retrieving relation by empty key
Browse files Browse the repository at this point in the history
  • Loading branch information
rkolpakov committed Feb 12, 2019
1 parent dce6a55 commit 4a75fa0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/attributes/relations/BelongsTo.ts
Expand Up @@ -94,7 +94,7 @@ export default class BelongsTo extends Relation {

collection.forEach((model) => {
const id = model[this.foreignKey]
const relation = dictionary[id]
const relation = id !== null ? dictionary[id] : null

model[name] = relation || null
})
Expand Down
46 changes: 46 additions & 0 deletions test/feature/relations/Retrieve_With.spec.js
Expand Up @@ -488,4 +488,50 @@ describe('Feature – Relations – Retrieve – With', () => {

expect(user.posts.length).toBe(0)
})

it('does not retrieve empty relation', async () => {
class User extends Model {
static entity = 'users'

static fields () {
return {
id: this.attr(null)
}
}
}

class Post extends Model {
static entity = 'posts'

static fields () {
return {
id: this.attr(null),
user_id: this.attr(null),
author: this.belongsTo(User, 'user_id')
}
}
}

createStore([{ model: User }, { model: Post }])

await User.create({
data: {
id: 1
}
})
await Post.create({
data: {
id: 1,
user_id: null
}
})

const post = Post.query()
.with('*')
.first()

expect(post.id).toEqual(1)
expect(post.user_id).toEqual(null)
expect(post.author).toEqual(null)
})
})

0 comments on commit 4a75fa0

Please sign in to comment.