【问题标题】:How to include model in sequelize without nesting the output如何在不嵌套输出的情况下在 sequelize 中包含模型
【发布时间】:2020-06-07 06:05:30
【问题描述】:

我有以下代码

    Locations.findAll({
    attributes: ['id', 'location_name'],
    include: [{
        model: Somemodel,
        attributes: ['hierarchy_name']
       }]
    })

这将提供如下输出:

    id: 1
    location_name: "City"
    parent_id: 0
    hierarchy: 1
    location_hierarchy: {hierarchy_name: "Division"}

有什么方法可以在不使用 sequelize 嵌套输出的情况下获得 hierarchy_name?像这样:

    id: 1
    location_name: "City"
    parent_id: 0
    hierarchy: 1
    hierarchy_name: "Division"

我知道我可以在将数据发送给客户端之前对其进行解构,我会得到我想要的格式。但我想知道这是否可以直接从 sequelize 中完成。

【问题讨论】:

    标签: postgresql express sequelize.js


    【解决方案1】:

    如果您希望显示大量数据集,而无需进行任何操作,则需要将选项 raw 设置为 true

    请参考以下问题-

    Setting all queries to raw = true sequelize

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 2019-04-11
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2014-09-23
      相关资源
      最近更新 更多