【问题标题】:Order Only Nested Model Data Sequelize仅订购嵌套模型数据序列化
【发布时间】:2020-02-04 01:49:18
【问题描述】:

我试图仅订购基于整数列的嵌套包含方案模型,但似乎代码不起作用。此外,数据从MYSQL 底部向上获取并显示为输出。

示例代码:

[err, products] = await to(
Product_Inventory_Mapping.findAll({
  include: [
    {
      model: Product,
      as: "product_details",
      where: filterClause,
      include: [
        {
          model: Unit,
          as: "unit_details"
        },
        {
          model: Brand,
          as: "brand_details"
        },
        {
          model: Category,
          as: "category_details"
        },
        {
          model: Sub_Brand,
          as: "sub_brand_details"
        },
        {
          model: Sub_Category,
          as: "sub_category_details"
        },
        {
          model: Brand_Company,
          as: "brand_company_details"
        },
        {
          model: Scheme,
          as: "schemes",
          attributes: [`moq`, 'discount', `description`],
          order: [[{ model: Scheme, as: "schemes" }, "moq", "ASC"]],
          where: {
            status: 1
          },
          required: false
        }
      ]
    }
  ],
  order: [
    [{ model: Product, as: "product_details" }, "mrp", "ASC"]
  ],
  where: {
    user_id: aligned_distributors,
    brand_company_id: filtered_brand_company,
    count: {
      [Op.gt]: 0
    },
    sp: {
      [Op.gt]: 0
    }
  },
  attributes: [
    `product_id`,
    "user_id",
    "count",
    "sp",
    "occ",
    "brand_company_id"
  ],
  limit: limit,
  offset: offset,
  subQuery: false
}));

这里我只想订购 Scheme 模型中的数据。 示例响应:

"products": [
    {
        "product_id": 115,
        "user_id": 1003,
        "count": 50,
        "sp": 9.09,
        "occ": 9.09,
        "brand_company_id": 11,
        "product_details": {
            "id": 115,
            "category_id": 2,
            "sub_category_id": 12,
            "brand_company_id": 11,
            "brand_id": 24,
            "sub_brand_id": 44,
            "sku_code": null,
            "min_order_qty": "27",
            "description": "Nandini Butter Milk Tetra Pack, 200 Ml",
            "unit_id": 4,
            "weight": "200ml",
            "mrp": "10.00",
            "barcode": "",
            "image_url": "https://xxxxxx.s3.ap-south-1.amazonaws.com/products/1565919229Unknown-min-5.jpg",
            "created_at": "2019-08-12T13:44:01.000Z",
            "updated_at": "2019-08-15T20:03:49.000Z",
            "unit_details": {
                "id": 4,
                "name": "Pieces",
                "created_at": null,
                "updated_at": null
            },
            "brand_details": {
                "id": 24,
                "name": "Nandini Butter Milk",
                "brand_company_id": 11,
                "created_at": null,
                "updated_at": null
            },
            "category_details": {
                "id": 2,
                "name": "Packaged Food",
                "image_url": "https://zzzz.s3.ap-south-1.amazonaws.com/images/category/food-min.png",
                "created_at": "2019-08-11T11:36:52.000Z",
                "updated_at": "2019-08-11T11:36:52.000Z"
            },
            "sub_brand_details": {
                "id": 44,
                "name": "Nandini Butter Milk",
                "brand_id": 24,
                "created_at": null,
                "updated_at": null
            },
            "sub_category_details": {
                "id": 12,
                "name": "Dairy products (Butter, Cheese etc)",
                "category_id": 2,
                "created_at": null,
                "updated_at": null
            },
            "brand_company_details": {
                "id": 11,
                "name": "Karnataka Co-operative Milk Producers",
                "image_url": "https://xxxxx.s3.ap-south-1.amazonaws.com/images/company/kmf_logo.jpg",
                "created_at": "2019-08-12T13:10:18.000Z",
                "updated_at": "2019-08-12T13:10:18.000Z"
            },
            "schemes": [
                {
                    "moq": 30,
                    "discount": 1.5,
                    "description": "8 % offer"
                },
                {
                    "moq": 20,
                    "discount": 1,
                    "description": "20 % offer"
                },
                {
                    "moq": 40,
                    "discount": 2,
                    "description": "40 % offer"
                }
            ]
        }
    }]

在数据库中,Scheme数据保存为:

方案通过 product_id 属于产品。我希望 Scheme 数组按 moq ASC 排序,以便值按 moq 顺序排列:20 > 30 > 40

【问题讨论】:

    标签: mysql node.js sequelize.js columnsorting


    【解决方案1】:

    AFAIK,包含模型中的 order 子句不起作用。但是,您可以在主模型中引用包含的模型。我认为如果您将两个 order 子句合并为一个,这应该会起作用:

    Product_Inventory_Mapping.findAll({
    include: [
      ..... lots of includes here ....
    ],      
    order: [
            [{ model: Product, as: "product_details" }, "mrp", "ASC"],
            [{ model: Scheme, as: "schemes" }, "moq", "ASC"]]
          ],
    

    这将在 mrp 中按 moq 排序...如果您真的只想按 moq 排序,则可以删除第一个字段...。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2014-05-30
      • 2020-08-24
      相关资源
      最近更新 更多