关于 node.js:如何在 sequelize 查询的 where 中使用关系? | 珊瑚贝

How can I use a relation in the where of a sequelize query?


我有两张表,我们称它们为 \\’person\\’ 和 \\’car\\’,关系如下。一个人可以有 1 辆或 0 辆汽车。在后一种情况下,Car 表中将没有条目。

1
2
3
4
5
6
7
8
Person:
  firstName,
  lastName,
  ….
  hasOne(Car, {
    foreignKey: ‘personId’,
    AS: ‘personsCar’
  })
1
2
3
4
5
6
7
8
Car:
  personId,
  colour,
 
  belongsTo(Person, {
    foreignKey: ‘personId’,
    AS: ‘carOwner’
  })

使用 Sequelize 我想从 Person 表中获取所有有”绿色”汽车或没有汽车的人

只是试图用以下任何颜色的汽车来找人会给出错误”列 Person.personsCar 不存在”,我如何才能在查询的位置正确引用关系?

1
2
3
4
5
6
7
8
9
10
11
12
Person.findAll({
  include: [
    model: Car,
    AS: ‘personsCar’,
    attributes: [‘colour’],
  ],
  WHERE: {
    personsCar: {
      [op.eq]: NULL,
    },
  },
})

你可以这样做:

1
2
3
4
5
6
7
8
9
10
Person.findAll({
    include: [
        model: Car,
        AS: ‘personsCar’,
        attributes: [‘colour’],
        WHERE: {
            colour : ‘green’ // <—- YOUR COLOUR
        },
    ],
})
  • 谢谢维维克!这是我所问的确切答案,但我错过了一个重要的标准,我想得到没有车的人。


来源:https://www.codenong.com/57684733/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9031.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?