Skip to content

How to filter an Association? #267

Closed
@peruzzo

Description

@peruzzo

I want to be able to filter a association, as in the example below only bring the emails assets of a particular user.

var User =  db.define('user', ...);
var Email = db.define('email', ...);
Email.hasOne('user', User, {required: true, reverse: 'emails'});


user.getEmails().find({active: true}).run(...);
user.getEmails().where("active = ?", [true]).all(...);

Activity

dresende

dresende commented on Aug 1, 2013

@dresende
Owner

If you are using hasOne, you only get one association, no reason to filter. If you're using hasMany you can do exactly what you're proposing in your examples.

peruzzo

peruzzo commented on Aug 1, 2013

@peruzzo
Author

My intention is actually the reverse filter I set the hasOne, I thought the reverse applied a hasMany the user for email.

You said I was supposed to use the hasMany, but I can use without having a join table?

dresende

dresende commented on Aug 1, 2013

@dresende
Owner

I have to think how to change the interface to allow a reverse hasOne to be a hasMany. There's currently no support for it, but this is on the top of my list.

taoyuan

taoyuan commented on Aug 5, 2013

@taoyuan

Maybe the phenomenon described in #281 has the same reason. The hasOne and hasMany make me confused. I'm looking forward that this problem is resolved.

starwing

starwing commented on Aug 5, 2013

@starwing

and #237 as well.

Maybe it's very easy one: in interface, just makes reversed getAccessor return a ChainFind, so we can filter/count/limit, etc. on it.

in theory, a hasOne is a 1-to-n relation, a table A has a B_id, it means table B may have many different A instance. so at the reversed case, it should be a n-to-1 relation.

hopes to see this is implement :-)

dresende

dresende commented on Aug 7, 2013

@dresende
Owner

@starwing is right, a reversed hasOne should behave very similar to a normal hasMany. I'm hoping to fix this until the end of next week. Hopefully I'll also add more detail in the wiki about the differences in associations.

Fauntleroy

Fauntleroy commented on Oct 24, 2013

@Fauntleroy

@dresende I've started to run into this issue in a variety of places. Has there been any progress on this?

dresende

dresende commented on Oct 24, 2013

@dresende
Owner

It depends on the place. Do you have examples?

Fauntleroy

Fauntleroy commented on Oct 24, 2013

@Fauntleroy

We have models User and Like. We have this association:

Like.hasOne( 'owner', User, {
    reverse: 'likes'
});

I think I should be able to use:

User.get( 1, function( err, user ){
    user.getLikes().limit(5).run( function( err, likes ){
        console.log( likes );
    });
});

But I can't since the limit method doesn't exist there.

dresende

dresende commented on Oct 30, 2013

@dresende
Owner

Please try this latest commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      How to filter an Association? · Issue #267 · dresende/node-orm2