【问题标题】:How to query virtual column using Sequelize?如何使用 Sequelize 查询虚拟列?
【发布时间】:2017-10-21 12:50:13
【问题描述】:

Student表中有一个Sage,我想用2017-Sage查询一个学生的出生年份,但是我不知道怎么做,我试过这样:

db.Student.findAll({
        attributes: ['Sname','Ssex',[2017-Sequelize.col('Sage'),'Year of birth']],
        where: {
            Clno: {
                $in: ['01311','10665']
            }
        }
    })

但它来了错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: attr[0].indexOf is not a function

【问题讨论】:

    标签: mysql node.js database sequelize.js


    【解决方案1】:

    您可能需要检查 Sequelize.VIRTUAL 数据类型。

    所以在学生模型定义中可能需要这样的东西:

    Student = sequelize.define('student', {
        ...
        birthYear: {
            type: Sequelize.VIRTUAL(Sequelize.INTEGER, "(2017 - `students`.age) as birthYear")
        }
    }
    

    更多示例可在此处获得:https://github.com/sequelize/sequelize/issues/7350

    【讨论】:

    • 使用刻度似乎不再起作用。相反,您可以将带有文字和别名的元组作为虚拟列所依赖的属性之一传递:Sequelize.VIRTUAL(Sequelize.INTEGER, [[Sequelize.literal("2017 - students.age"), "birthYear"]])
    猜你喜欢
    • 2019-11-09
    • 1970-01-01
    • 2018-03-04
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多