nodejs sequelize包含关系排序

时间:2019-10-09 05:48:47

标签: node.js postgresql orm sequelize.js

有两个模型,一个是船长,另一个是船价。我希望所有船数据及其价格和顺序都通过降船价来查询,该查询无法按预期运行,其排序数据在我的关系中,结果显示如下排序,但没有选择价格最高的船

db.BoatMaster.findAndCountAll({
    distinct: true,
    limit:request.params.pagesize,
    offset:offsetpage,
    attributes:[
        'id',
        'name',
        'saleprice','length','cabins','year','berths','createdAt','updatedAt'
    ],
    include:[
        "boat_price",

    ],
    order:[
        ['boat_price','price','DESC']
    ],
})

这是查询输出

SELECT 
  "BoatMaster".*, 
  "boat_price"."id" AS "boat_price.id", 
  "boat_price"."price" AS "boat_price.price", 
  "boat_price"."boatId" AS "boat_price.boatId", 
  "boat_price"."currency" AS "boat_price.currency", 
  "boat_price"."datefrom" AS "boat_price.datefrom", 
  "boat_price"."dateto" AS "boat_price.dateto" 
FROM 
  (
    SELECT 
      "BoatMaster"."id", 
      "BoatMaster"."name", 
      "BoatMaster"."saleprice", 
      "BoatMaster"."length", 
      "BoatMaster"."cabins", 
      "BoatMaster"."year", 
      "BoatMaster"."berths", 
      "BoatMaster"."createdAt", 
      "BoatMaster"."updatedAt" 
    FROM 
      "BoatMasters" AS "BoatMaster" 
    WHERE 
      (
        SELECT 
          "boatId" 
        FROM 
          "boatprices" AS "boat_price" 
        WHERE 
          (
            "boat_price"."boatId" = "BoatMaster"."id"
          ) 
        LIMIT 
          1
      ) IS NOT NULL 
    LIMIT 
      10 OFFSET 0
  ) AS "BoatMaster" 
  INNER JOIN "boatprices" AS "boat_price" ON "BoatMaster"."id" =     "boat_price"."boatId" 
ORDER BY 
  "boat_price"."price" DESC;

有人可以帮我解决我在这里做错的事情吗,因为数据没有按照价格排序? 多谢

1 个答案:

答案 0 :(得分:0)

尝试此操作(如果正确):

 include:[
        boat_price,

    ],
    order:[
        [boat_price,'price','DESC']
    ],

 include:[
        boat_price,

    ],
    order:[
        ['boat_price.price','DESC']
    ],