续集找到。如何在查找查询中包含常量值属性

Sequelize find. how to include constant value attributes in a find query

¿如何从 table 中检索所有属性,再加上一个新的 column/attribute,每行都有一个常量值? 正如我在文档中看到的那样,它应该与此类似:

my_model.findAll({
    attributes:{
        include:[['constant_value','new_attribute_name']]
    }
}).then(...);

有线索吗?

谢谢!

属性对象专门针对模型中已有的 select 个属性。

据我所知,您无法向模型添加属性,但可以向模型添加方法。

查看这些方法的文档here

例如,您可以创建一个简单的 class/instance 方法,它只是 returns 常量。

您还可以创建一个 hook,在检索到常量后将其添加到模型中。

sequelize中可以使用字面量函数

my_model.findAll({
    attributes:{
        include: [[Sequelize.literal(`"constant_value"`,'new_attribute_name']]
    }
}).then(...);

我刚刚看到这个问题是很久以前发布的,但我希望这对其他人有所帮助。谢谢