Skip to content

Fix for issue #629#1716

Merged
ricardograca merged 1 commit into
bookshelf:masterfrom
syangdev:feature/fix-issue-629
Dec 29, 2017
Merged

Fix for issue #629#1716
ricardograca merged 1 commit into
bookshelf:masterfrom
syangdev:feature/fix-issue-629

Conversation

@syangdev

@syangdev syangdev commented Dec 28, 2017

Copy link
Copy Markdown

Can't attach belongsToMany relation to models fetched with fetchAll
#629

PR name

  • Related Issues: 629
  • Previous PRs: N/A

Introduction

Can't attach "belongsToMany" relation to models fetched with fetchAll. Will run to errors when the parent model access the collections of the child models and tries to detach.

Motivation

A many to many with a pivot table is pretty common. This bug makes bookshelf useless in this scenario.

Proposed solution

The current workaround documented in #629 is to load the child models again which is not very database friendly. The simple solution is to have the parent model recreate the the relation related data when pairing the child models with the parent models (note that before the child model fetch, a interim relation object was created and so the parent model couldn't be associated downstream of this process).

Fixes #629.

Current PR Issues

Not sure.

Alternatives considered

This seems like it would also affect many to many relationships.

Can't attach belongsToMany relation to models fetched with fetchAll
bookshelf#629
@ricardograca

Copy link
Copy Markdown
Member

This just needs tests. I can add tests myself if you don't have the time for that.

@syangdev

Copy link
Copy Markdown
Author

@ricardograca Yea, I don't have time. This is also my first time collaborating on open source project too, so forgive me. I just wanted to show a simple solution. Please note that this bug probably affects a many to many relationship too.

@ricardograca ricardograca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be writing some tests for this after it's merged.

@ricardograca ricardograca changed the title Fix for issue: Fix for issue #629 Dec 29, 2017
@ricardograca
ricardograca merged commit c37a28b into bookshelf:master Dec 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants