Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasOne().through() attach and joining issue #162

Closed
johanneslumpe opened this issue Jan 4, 2014 · 9 comments
Closed

hasOne().through() attach and joining issue #162

johanneslumpe opened this issue Jan 4, 2014 · 9 comments
Labels

Comments

@johanneslumpe
Copy link
Contributor

Spent some time trying to figure this out, but couldn't figure it out.

I have comments, which are related to different entities through pivot tables with foreign keys, to retain referential integrity.

I could easily get the entity for each comment, by setting up a belongsToMany relation. But this returns a collection and I would have to always check the collection length and then select the first element. Of course this is not the end of the world, but it would be nice if it was possible to set up a hasMany through relationship.

Checking the tests I saw that this type expects a table with an id and a foreign key column, but my schema has two foreign key columns, e.g. entity_id and comment_id.

I tried to pass in different keys as params to the relation methods but in one way or another it never worked out, as the produced sql always tries to join on pivottable.id, which for my purpose should be pivottable.entity_id.

So I tried to set up a hasOne relation on the Comment model with through like this:

this.hasOne(SomeEntityModel).through(CommentSomeEntityJoin, 'id');

Am I making any sense here? What I basically is a belongsToMany relation with a limit of 1, which does not return a collection but a model. So a hasOne relation, which uses a pivot table with the structure of a belongsToMany pivot table.

Am I missing something obvious?

On a side note: Attaching to a hasOne through relation was inserting into the database correctly using the following sql:

insert into `comments_entity` (`comment_id`, `entity_id`) values (?, ?)

After that Bookshelf tries to add the model to the collection which is a model in the case of a hasOne relation. Because it does not have an add method an error is thrown. I suppose I'm hitting an edge case here?!

@johanneslumpe
Copy link
Contributor Author

bump, anyone? ;)

@tgriesser
Copy link
Member

After that Bookshelf tries to add the model to the collection which is a model in the case of a hasOne relation. Because it does not have an add method an error is thrown. I suppose I'm hitting an edge case here?!

That sounds like a bug...

Am I making any sense here? What I basically is a belongsToMany relation with a limit of 1, which does not return a collection but a model. So a hasOne relation, which uses a pivot table with the structure of a belongsToMany pivot table.

If that works, then go for it... I'm thinking there should be a way of flexibly defining joins/relations outside of the ones provided... which would make creating these types of complex associations easier (related #127)

@johanneslumpe
Copy link
Contributor Author

That's the point - the hasOnewith through does unfortunately not work. Any help here is greatly appreciated. On a side node, since you mentioned joins: joining multiple tables as a condition for fetching a single model (restricting the result through an existing relation), the id column of the model being fetched is not being prefixed with the table name in th select or where part of the query and will then result in column id is ambiguous - ever had that issue?

@tgriesser
Copy link
Member

Oh... actually, what if you do a belongsToMany relation, but you use the fetchOne method?

@johanneslumpe
Copy link
Contributor Author

Oh let me try that!

@johanneslumpe
Copy link
Contributor Author

Yep, that works! Thanks for the idea. Now the other issue then is the prefixing of the ids, but I will open another issue for that.

@johanneslumpe
Copy link
Contributor Author

@tgriesser can you mark this one as a bug? Even though using belongsToMany with fetchOne works in some situations, it specifically doesn't work when eager-loading relations. Apart from that, there is the bug with trying to add something to a collection, when there is none ;)

@pschuegr
Copy link
Contributor

+1. I'm missing the ability to use hasOne/through. Is there a workaround which lets you still retrieve

model.fetch({withRelated: ['hasOneThroughRelation']})

?

@crunchtime-ali
Copy link
Member

The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year.

If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it.

We also started an open discussion about the future of Bookshelf.js here #1600. Feel free to drop by and give us your opinion.
Let's make Bookshelf great again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants