node.js - Sequelize finder 排除了属性但想要所有属性

标签 node.js sequelize.js

我将 sequelize 与 nodeJs 一起使用。
我有一个带有外键的表,但我不知道为什么,它们没有出现在查询中。

import { Model, DataTypes } from "sequelize"
import { sequelize } from "./config"

class VolumeRangeDiscount extends Model{
    public readonly id: number;
    public readonly updatedAt!:Date;
    public readonly createdAt!:Date;
    public discount: number;

    public static associate(models){
        VolumeRangeDiscount.belongsTo(models.VolumeRange, {
            foreignKey: "volumeRangeId",
            as: "volumeRange",
        });
        VolumeRangeDiscount.belongsTo(models.Pack, {
            foreignKey: "packId",
            as: "pack",
        });
    }
}

VolumeRangeDiscount.init({
    id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER,
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
    },
    discount:{
        type: DataTypes.INTEGER,
        allowNull: true,
    }
    
},  { sequelize, tableName: "VolumeRangeDiscount" })



export default VolumeRangeDiscount
当我想通过 id 查找时,它不会查询外键列:
export function findVolumeRangeDiscountById(id) {
    return VolumeRangeDiscount.findOne({ where: { id }, raw: true });
}
生成的查询是:
SELECT "id", "createdAt", "updatedAt", "discount" FROM "VolumeRangeDiscount" AS "VolumeRangeDiscount" WHERE "VolumeRangeDiscount"."id" = '3';
但是如果我添加属性选项和它工作的列名,它们会被返回。
您知道为什么默认情况下它们不在生成的查询中吗?
它不会在我的模型中添加外键,VolumeRangeDiscount.rawAttributes 返回:
 {
    id: {allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: INTEGER {
    options: {
    },
    _length: undefined,
        _zerofill: undefined,
        _decimals: undefined,
        _precision: undefined,
        _scale: undefined,
        _unsigned: undefined},
    Model: VolumeRangeDiscount,
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id'
},
createdAt: {type: DATE {options: [Object], _length: ''
},
    allowNull: false,
        Model: VolumeRangeDiscount,
        fieldName: 'createdAt',
        _modelAttribute: true,
        field: 'createdAt'},
updatedAt: { type: DATE {options: [Object], _length: ''
},
    allowNull: false,
        Model: VolumeRangeDiscount,
        fieldName: 'updatedAt',
        _modelAttribute: true,
        field: 'updatedAt'},
discount: {type: INTEGER {
    options: {},
    _length: undefined,
        _zerofill: undefined,
        _decimals: undefined,
        _precision: undefined,
        _scale: undefined,
        _unsigned: undefined
},
    allowNull: true,
        Model: VolumeRangeDiscount,
        fieldName: 'discount',
        _modelAttribute: true,
        field: 'discount'},
}

最佳答案

Sequelize 是一个 ORM,它为您处理关联。您可以使用它以两种方式获取关联行,而不是获取外键:

  • 急切加载
    VolumeRangeDiscount.findOne({ where: { id }, include 'VolumeRange' });
    
    这将使用关联的行填充 VolumeRange 对象。
  • 懒加载
     const vrDiscount = VolumeRangeDiscount.findOne({ where: { id }, include 'VolumeRange' });
     const volumeRange = vrDiscount.getVolumeRange();
    

  • 可以在 docs here 中找到更多关于此的信息。

    关于node.js - Sequelize finder 排除了属性但想要所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64461211/

    相关文章:

    mysql - 在 sequelize 中使用 where 子句 with 或 condition

    sql - Sequelize - 获得零售商后将商店并入零售商

    Node.js 集群与多个 Node.js 实例

    node.js - Node js Express中 'app.resource'有什么用

    node.js - 我在 ytdl-core 包上收到 404 错误

    javascript - 如何在if block 中处理异步函数

    node.js - 使用 sequelize 和环境变量连接数据库的 Node 配置文件设置

    javascript - 与一个外键关联序列化

    javascript - Node.js Cheerio 模块 ge

    javascript - FabricJS 从服务器端的路径加载图像?