ackerchez
ackerchez

Reputation: 1756

Sequelize hasMany with "Where"

I am coming from Laravel where you can work with Eloquent. In Eloquent you can do something like:

$this->hasOne('App\Models\Member', 'family_id')->where('relation', '=', 1);

In order to get a specific member within a wider scope.

Is the same thing possible to do in Sequelize? I see I can do

Family.hasMany(models.Member);

or

Family.hasOne(models.Member);

but I don't see where I Can add a constraint. Any ideas?

Upvotes: 2

Views: 5217

Answers (1)

ackerchez
ackerchez

Reputation: 1756

In order to accomplish this. The where clause is moved to the controller and not predefined in the model. It would be done like this:

models.Family.findAll({
    include:[
        {
            model : models.Member,
            where: { relation : 1 }
        },
    ]
})

Upvotes: 3

Related Questions