简体   繁体   English

Sequelize hasMany有“哪里”

[英]Sequelize hasMany with “Where”

I am coming from Laravel where you can work with Eloquent. 我来自Laravel,在这里您可以与Eloquent合作。 In Eloquent you can do something like: 在Eloquent中,您可以执行以下操作:

$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? 在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? 有任何想法吗?

In order to accomplish this. 为了做到这一点。 The where clause is moved to the controller and not predefined in the model. where子句将移至控制器,并且未在模型中预定义。 It would be done like this: 可以这样完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM