简体   繁体   中英

Sequelize get results when aggregate function is null

I have the following code which returns customers. The problem is that I get results only for customers that have placed an order and have created an address.

CustomerModel.findAll({
    attributes: {
      include: [
        [db.sequelize.fn("COUNT", db.sequelize.col("orders.id")), "orderCount"],
        [db.sequelize.literal(`(
          SELECT o.createdDate
          FROM orders AS o
          WHERE
              o.customerId = customer.id
          ORDER BY o.createdDate DESC
          LIMIT 1
      )`),
      'latestOrderDate'],
      [db.sequelize.fn('sum', db.sequelize.col('orders.amount')), 'totalAmount'],
      ]
    },
    include: [
      { model: UserModel, as: 'user' },
      { model: AddressModel, required:false },
      { model: OrderModel, attributes: [], required:false }
    ]
  })

Is it possible to get all customers regardless of having an order or an Address?

Thank you!

I ended up changing the two sequelize.fn to literal and it worked like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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