繁体   English   中英

续集如何在结果中返回连接表的列

[英]Sequelize how to return column of joined table in the results

我正在为我的 node.js mvc 项目使用 sequelize 模块,我想执行的查询如下

SELECT answer_text, isNew, name FROM answer JOIN topic ON answer.topic_id = topic.id

answer_textisNew是答案表的列,而name是仅存在于主题表中的列。

如何让主题表name列出现在isNew列旁边的结果中,以便我可以轻松访问它? sequelize 是否提供这样的功能,或者我有责任格式化结果?

我尝试在“topic.name”之类的属性中添加各种东西,但都没有奏效。

我设置文件结构的方式是基于他们的文档Sequelize usage with express

var models = require('../models')

var answers = await models.Answer.findAll({
    include: [{
        model: models.Topic
    }],
    attributes: [
        'answer_text',
        'isNew'
    ]
})
console.log(answers)

下面的output是

{ answer_text: 'maybe it is robots',
  isNew: true,
  Topic:
   Topic {
     dataValues:
      { id: 830,
        mid: 'm.0bjmp5',
        name: 'Robot Arena',
        description:
         'Robot Arena is a computer game made by Infogrames. It features robotic combat similar to that of Battlebots Robotica and Robot Wars. There are a number of different chassis and on top of that there are numerous attachments. Weapons accessories tires and other forms of mobility batteries and air tanks are among the customization choices. A sequel called Robot Arena 2 Design and Destroy was made which allows for total customization of your 
robot.',
        type: 'cvg.computer_videogame' },
     _previousDataValues:
      { id: 830,
        mid: 'm.0bjmp5',
        name: 'Robot Arena',
        description:
         'Robot Arena is a computer game made by Infogrames. It features robotic combat similar to that of Battlebots Robotica and Robot Wars. There are a number of different chassis and on top of that there are numerous attachments. Weapons accessories tires and other forms of mobility batteries and air tanks are among the customization choices. A sequel called Robot Arena 2 Design and Destroy was made which allows for total customization of your 
robot.',
        type: 'cvg.computer_videogame' },
     _changed: {},
     _modelOptions:
      { timestamps: false,
        validate: {},
        freezeTableName: false,
        underscored: false,
        paranoid: false,
        rejectOnEmpty: false,
        whereCollection: null,
        schema: null,
        schemaDelimiter: '',
        defaultScope: {},
        scopes: {},
        indexes: [],
        name: [Object],
        omitNull: false,
        sequelize: [Sequelize],
        hooks: {} },
     _options:
      { isNewRecord: false,
        _schema: null,
        _schemaDelimiter: '',
        include: undefined,
        includeNames: undefined,
        includeMap: undefined,
        includeValidated: true,
        raw: true,
        attributes: undefined },
     isNewRecord: false } }

请尝试以下 sequelize 语句 -

var answers = await models.Answer.findAll({
    include: [{
        model: models.Topic,
    attributes: ['name']
    }],
    attributes: [
        'answer_text',
        'isNew'
    ],
    raw: true
})

我希望它有帮助!

工作答案:

为了在属性中使用[sequelize.col('Topic.name'), 'name']必须需要 Sequelize,以便我们可以获取 Topic 表的name列并将 'Topics.name' 重命名为 name。 (试过models.col但它不是一个函数)

raw: true如果您只想获取answers[0]中的列,则需要 true

attributes:[]在 include 中是必需的,因为如果你不放它,结果将包括连接表(主题)中的所有列。

const models = require('../models')
const sequelize = require('sequelize');

var answers = await models.Answer.findAll({
                include: [{
                    model: models.Topic,
                    attributes: []
                }],
                attributes: [
                    'answer_text',
                    'isNew',
                    [sequelize.col('Topic.name'), 'name']
                ],
                raw: true
            })
console.log(answers[0])

output:

{ answer_text: 'robot arena',
  isNew: 'true',
  name: 'Robot Arena' }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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